Challenge - 5 Problems
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def removeNthFromEnd(head, n): dummy = ListNode(0, head) first = dummy second = dummy for _ in range(n + 1): first = first.next while first: first = first.next second = second.next second.next = second.next.next return dummy.next # Create linked list 1 -> 2 -> 3 -> 4 -> 5 -> null head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))) new_head = removeNthFromEnd(head, 2) # Print list result = [] while new_head: result.append(new_head.val) new_head = new_head.next print(result)
Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the 4th node from the start in a 5-node list.
✗ Incorrect
Removing the 2nd node from the end removes the node with value 4. The resulting list is 1 -> 2 -> 3 -> 5 -> null.
❓ Predict Output
intermediate2:00remaining
Output after removing last node
What is the output linked list after removing the 1st node from the end (last node) in the given list?
DSA Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def removeNthFromEnd(head, n): dummy = ListNode(0, head) first = dummy second = dummy for _ in range(n + 1): first = first.next while first: first = first.next second = second.next second.next = second.next.next return dummy.next # Create linked list 10 -> 20 -> 30 -> null head = ListNode(10, ListNode(20, ListNode(30))) new_head = removeNthFromEnd(head, 1) # Print list result = [] while new_head: result.append(new_head.val) new_head = new_head.next print(result)
Attempts:
2 left
💡 Hint
Removing the last node means removing the node with value 30.
✗ Incorrect
The last node (value 30) is removed, so the list becomes 10 -> 20 -> null.
🔧 Debug
advanced2:00remaining
Identify the error in removing nth node from end
What error will this code raise when trying to remove the 3rd node from the end of a 2-node list?
DSA Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def removeNthFromEnd(head, n): dummy = ListNode(0, head) first = dummy second = dummy for _ in range(n): first = first.next while first.next: first = first.next second = second.next second.next = second.next.next return dummy.next # Create linked list 1 -> 2 -> null head = ListNode(1, ListNode(2)) new_head = removeNthFromEnd(head, 3)
Attempts:
2 left
💡 Hint
Check the for loop that moves the first pointer n times.
✗ Incorrect
The for loop tries to move first pointer 3 times, but the list has only 2 nodes plus dummy, so first becomes None and accessing first.next causes AttributeError on NoneType.
🧠 Conceptual
advanced1:00remaining
Why use a dummy node in removing nth node from end?
What is the main reason to use a dummy node before the head when removing the nth node from the end of a linked list?
Attempts:
2 left
💡 Hint
Think about what happens if the node to remove is the first node.
✗ Incorrect
Using a dummy node allows uniform handling of all nodes including the head, so removing the head node doesn't require special code.
❓ Predict Output
expert3:00remaining
Output after removing 4th node from end in a 5-node list
What is the output linked list after removing the 4th node from the end in the list 5 -> 4 -> 3 -> 2 -> 1 -> null?
DSA Python
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def removeNthFromEnd(head, n): dummy = ListNode(0, head) first = dummy second = dummy for _ in range(n + 1): first = first.next while first: first = first.next second = second.next second.next = second.next.next return dummy.next # Create linked list 5 -> 4 -> 3 -> 2 -> 1 -> null head = ListNode(5, ListNode(4, ListNode(3, ListNode(2, ListNode(1))))) new_head = removeNthFromEnd(head, 4) # Print list result = [] while new_head: result.append(new_head.val) new_head = new_head.next print(result)
Attempts:
2 left
💡 Hint
The 4th node from the end is the 2nd node from the start.
✗ Incorrect
Removing the 4th node from the end removes the node with value 4. The resulting list is 5 -> 3 -> 2 -> 1 -> null.