Challenge - 5 Problems
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after removing 2nd node from end
What is the output linked list after removing the 2nd node from the end in the given list?
DSA C#
public class ListNode { public int val; public ListNode next; public ListNode(int val=0, ListNode next=null) { this.val = val; this.next = next; } } public class Solution { public ListNode RemoveNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0, head); ListNode first = dummy; ListNode second = dummy; for (int 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; } } // Input list: 1 -> 2 -> 3 -> 4 -> 5 -> null // n = 2 // Call: RemoveNthFromEnd(head, 2)
Attempts:
2 left
💡 Hint
Think about how the two pointers move and which node gets skipped.
✗ Incorrect
The 2nd node from the end is '4'. Removing it results in the list: 1 -> 2 -> 3 -> 5 -> null.
❓ Predict Output
intermediateOutput after removing 1st node from end
What is the output linked list after removing the last node (1st from end) in the given list?
DSA C#
List: 10 -> 20 -> 30 -> 40 -> null n = 1 Call RemoveNthFromEnd(head, 1)
Attempts:
2 left
💡 Hint
Removing the last node means the node with value 40 is removed.
✗ Incorrect
Removing the last node (40) leaves the list: 10 -> 20 -> 30 -> null.
🔧 Debug
advancedIdentify the error in this RemoveNthFromEnd implementation
What error will this code cause when removing the 3rd node from end in a 3-node list?
DSA C#
public ListNode RemoveNthFromEnd(ListNode head, int n) { ListNode first = head; ListNode second = head; for (int i = 0; i < n; i++) { first = first.next; } while (first.next != null) { first = first.next; second = second.next; } second.next = second.next.next; return head; } // List: 1 -> 2 -> 3 -> null // n = 3 // Call RemoveNthFromEnd(head, 3)
Attempts:
2 left
💡 Hint
Check what happens when first pointer moves n steps ahead without dummy node.
✗ Incorrect
When n equals list length, first becomes null after the loop, so first.next causes NullReferenceException.
🧠 Conceptual
advancedWhy use a dummy node in RemoveNthFromEnd?
What is the main reason to use a dummy node before the head in the RemoveNthFromEnd algorithm?
Attempts:
2 left
💡 Hint
Think about what happens if the node to remove is the first node.
✗ Incorrect
The dummy node allows uniform handling of node removal, including when the head node must be removed, avoiding special cases.
🚀 Application
expertResulting list after multiple RemoveNthFromEnd calls
Given the initial list 5 -> 10 -> 15 -> 20 -> 25 -> null, what is the list after these calls?
1) RemoveNthFromEnd(head, 3)
2) RemoveNthFromEnd(head, 1)
3) RemoveNthFromEnd(head, 3)
Attempts:
2 left
💡 Hint
Apply each removal step carefully, updating the list each time.
✗ Incorrect
Step 1 removes 15 (3rd from end), list: 5 -> 10 -> 20 -> 25
Step 2 removes 25 (last node), list: 5 -> 10 -> 20
Step 3 removes 5 (3rd from end, which is head), list: 10 -> 20
Final list: 10 -> 20 -> null
