Challenge - 5 Problems
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of removing 2nd node from end in a 5-node list
What is the printed linked list after removing the 2nd node from the end?
DSA Javascript
class ListNode { constructor(val, next = null) { this.val = val; this.next = next; } } function removeNthFromEnd(head, n) { let dummy = new ListNode(0, head); let first = dummy; let second = dummy; for (let i = 0; i <= n; i++) { first = first.next; } while (first !== null) { first = first.next; second = second.next; } second.next = second.next.next; return dummy.next; } // Create list: 1 -> 2 -> 3 -> 4 -> 5 -> null let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))); head = removeNthFromEnd(head, 2); // Print list let output = []; let current = head; while (current !== null) { output.push(current.val); current = current.next; } console.log(output.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the 4th node from the start in a 5-node list.
✗ Incorrect
The 2nd node from the end is the node with value 4. Removing it results in the list: 1 -> 2 -> 3 -> 5 -> null.
❓ Predict Output
intermediateOutput after removing last node from a 3-node list
What is the printed linked list after removing the 1st node from the end (last node) in a 3-node list?
DSA Javascript
class ListNode { constructor(val, next = null) { this.val = val; this.next = next; } } function removeNthFromEnd(head, n) { let dummy = new ListNode(0, head); let first = dummy; let second = dummy; for (let i = 0; i <= n; i++) { first = first.next; } while (first !== null) { first = first.next; second = second.next; } second.next = second.next.next; return dummy.next; } // Create list: 10 -> 20 -> 30 -> null let head = new ListNode(10, new ListNode(20, new ListNode(30))); head = removeNthFromEnd(head, 1); // Print list let output = []; let current = head; while (current !== null) { output.push(current.val); current = current.next; } console.log(output.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Removing the last node means the node with value 30 is removed.
✗ Incorrect
The last node (value 30) is removed, so the list becomes 10 -> 20 -> null.
🔧 Debug
advancedIdentify the error in this Remove Nth Node from End code
What error does this code produce when trying to remove the 5th node from the end in a 4-node list?
DSA Javascript
class ListNode { constructor(val, next = null) { this.val = val; this.next = next; } } function removeNthFromEnd(head, n) { let dummy = new ListNode(0, head); let first = dummy; let second = dummy; for (let i = 0; i < n; i++) { first = first.next; } while (first.next !== null) { first = first.next; second = second.next; } second.next = second.next.next; return dummy.next; } // Create list: 5 -> 6 -> 7 -> 8 -> null let head = new ListNode(5, new ListNode(6, new ListNode(7, new ListNode(8)))); head = removeNthFromEnd(head, 5);
Attempts:
2 left
💡 Hint
Check the for loop that moves the first pointer n steps ahead.
✗ Incorrect
The for loop moves first pointer only n times instead of n+1, so first becomes null too early. Accessing first.next causes TypeError.
🧠 Conceptual
advancedWhy use a dummy node in Remove Nth Node from End?
Why do we use a dummy node before the head in the Remove Nth Node from End algorithm?
Attempts:
2 left
💡 Hint
Think about what happens if the first node is removed.
✗ Incorrect
The dummy node acts as a safe starting point so that removing the head node doesn't require special handling.
❓ Predict Output
expertOutput after removing 4th node from end in a 6-node list
What is the printed linked list after removing the 4th node from the end in this 6-node list?
DSA Javascript
class ListNode { constructor(val, next = null) { this.val = val; this.next = next; } } function removeNthFromEnd(head, n) { let dummy = new ListNode(0, head); let first = dummy; let second = dummy; for (let i = 0; i <= n; i++) { first = first.next; } while (first !== null) { first = first.next; second = second.next; } second.next = second.next.next; return dummy.next; } // Create list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6)))))); head = removeNthFromEnd(head, 4); // Print list let output = []; let current = head; while (current !== null) { output.push(current.val); current = current.next; } console.log(output.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
The 4th node from the end is the node with value 3.
✗ Incorrect
Removing the 4th node from the end removes the node with value 3, resulting in the list: 1 -> 2 -> 4 -> 5 -> 6 -> null.
