Remove N-th Node From End
Two-pointer technique to remove nth node from end
Remove Nth From End (Python)
6 slow = dummy● 8 # Move fast n steps ahead......10 fast = fast.next11 12 # Move both until fast reaches end......14 slow = slow.next15 fast = fast.next16 ......18 slow.next = slow.next.next19 20 return dummy.nextCurrent Line (7): Empty line
Remove Nth From End
INITIALIZE
Step
0 / 8
Phase
Initialize
N
2
Target
—
Algorithm Progress
Pointer State
Step Explanation
SetupInitialize Pointers
Create dummy node (0) pointing to head. Fast starts at head, slow at dummy.
- > Phase: Setup
- > dummy = ListNode(0, head)
- > n = 2: remove 2nd node from end
- > Dummy node ensures we can remove head without special case
- > Dummy node ensures we can remove head without special case.
Unvisited
Current (Slow)
Done
Fast Pointer