0
0
DSA Pythonprogramming~10 mins

Remove Nth Node from End of List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Remove Nth Node from End of List
Create dummy node pointing to head
Set two pointers: first and second at dummy
Move first pointer n+1 steps ahead
Move first and second pointers together until first reaches end
Second pointer now before target node
Remove target node by changing second.next
Return dummy.next as new head
We use two pointers spaced n+1 apart to find the node before the target, then remove the target node.
Execution Sample
DSA Python
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
Removes the nth node from the end of a singly linked list using two pointers.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create dummy node pointing to head0 -> 1 -> 2 -> 3 -> 4 -> 5 -> nulldummy -> 0, dummy.next -> 1dummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> null
2Set first and second pointers at dummy0 -> 1 -> 2 -> 3 -> 4 -> 5 -> nullfirst = dummy(0), second = dummy(0)dummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> null
3Move first pointer n+1=3 steps ahead0 -> 1 -> 2 -> 3 -> 4 -> 5 -> nullfirst moves: 0->1, 1->2, 2->3dummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> null
4Move first and second pointers together until first reaches end0 -> 1 -> 2 -> 3 -> 4 -> 5 -> nullfirst: 3->4, 4->5, 5->null; second: 0->1, 1->2, 2->3dummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> null
5Second pointer now before target node (3)0 -> 1 -> 2 -> 3 -> 4 -> 5 -> nullsecond at node 3dummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> null
6Remove target node by changing second.next0 -> 1 -> 2 -> 3 -> 4 -> 5 -> nullsecond.next changes from 4 to 5 (skips 4)dummy(0) -> 1 -> 2 -> 3 -> 5 -> null
7Return dummy.next as new head1 -> 2 -> 3 -> 5 -> nullhead updated to node 11 -> 2 -> 3 -> 5 -> null
💡 first pointer reached end (null), loop ends, target node removed
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
dummynode(0) -> 1node(0) -> 1node(0) -> 1node(0) -> 1node(0) -> 1
firstnode(0)node(3)nullnullnull
secondnode(0)node(0)node(3)node(3)node(3)
headnode(1)node(1)node(1)node(1)node(1)
List Length66655
Key Moments - 3 Insights
Why do we create a dummy node before the head?
The dummy node helps handle cases where the head itself is removed. It simplifies pointer changes by always having a node before the target. See Step 1 and Step 7 in execution_table.
Why move the first pointer n+1 steps ahead, not just n?
Moving first pointer n+1 steps ahead keeps the gap so that when first reaches the end, second is just before the node to remove. This is shown in Step 3 and Step 4.
What happens if n equals the length of the list?
Then the node to remove is the head. The dummy node allows second pointer to be before head, so removal works the same. This is why dummy is important (Step 1 and Step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, where is the first pointer after moving n+1 steps?
AAt node 2
BAt node 3
CAt node 4
DAt dummy node
💡 Hint
Check the 'Pointer Changes' column in Step 3 of execution_table.
At which step does the second pointer move to the node before the target node?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for when 'second at node 3' appears in execution_table.
If we remove the 1st node from the end (n=1), how would Step 6's pointer change differ?
Afirst pointer would not move
Bsecond.next would skip the head node
Csecond.next would skip the last node
Ddummy node would be removed
💡 Hint
Think about which node is removed when n=1 and check Step 6 pointer change.
Concept Snapshot
Remove Nth Node from End of List:
- Use dummy node before head
- Set two pointers at dummy
- Move first pointer n+1 steps ahead
- Move both pointers until first reaches end
- second.next skips target node
- Return dummy.next as new head
Full Transcript
This concept removes the nth node from the end of a singly linked list. We start by creating a dummy node pointing to the head to handle edge cases. Two pointers, first and second, start at the dummy. We move the first pointer n+1 steps ahead to maintain a gap. Then we move both pointers forward until the first reaches the end. At this point, the second pointer is just before the node to remove. We remove the target node by changing second.next to skip it. Finally, we return dummy.next as the new head of the list. This method works even if the head node is removed.