Remove Nth Node from End

medium

Remove the nth node from the end of a linked list in one pass

Remove Nth Node From End

Key Insight

Create an N-node gap between two pointers, then the trailing pointer lands at the target

Step 1Setup with Dummy
n = 2
fast
slow
D
1
2
3
4
5
null

Add dummy before head. Both pointers start at dummy. n=2 means remove 2nd from end.

1 / 6
Practice the Code

Step-by-Step Walkthrough: Remove Nth Node From End

Create an N-node gap between two pointers, then the trailing pointer lands at the target

  1. Setup with Dummy

    Add dummy before head. Both pointers start at dummy. n=2 means remove 2nd from end.

  2. Advance Fast by N

    Move fast 2 steps ahead to create the gap.

  3. Move Both Together

    Move both pointers one step at a time until fast reaches the end.

  4. Fast at End

    Fast at node 5 (last). Slow is at node 3, one before the target.

  5. Remove Node

    Set slow.next = slow.next.next, skipping node 4.

  6. Result

    Node 4 removed. Return dummy.next as new head.