Linked Lists — Visual Learning

Remove Nth Node from End —
Fixed gap. One pass. Exact position.

Remove the Nth node from the end of a linked list in one pass. Two pointers maintain a fixed gap of N nodes — when the front pointer reaches the end, the back pointer is exactly at the node before the one to remove. Watch the gap-based two-pointer dance on LearnBug and the one-pass trick becomes impossible to forget.

O(n)Time — one pass
O(1)Space complexity
PythonLanguage

The gap trick

To find the Nth node from the end in one pass, create a gap of exactly N between two pointers. Advance fast N steps ahead. Then move both fast and slow together until fast hits None. At that point, slow is exactly at the node before the target — ready to skip it.

Why visualizing the gap matters

Off-by-one errors are the main failure mode here — whether fast is N or N+1 steps ahead changes which node slow lands on. On LearnBug you see the gap between pointers counted explicitly, and the deletion step is visible as slow.next = slow.next.next skipping the target node.

YouTube — Remove Nth Node Explained
📺 Drop your YouTube embed here
LearnBug — gap pointers advancing to deletion point
🖼 Add a LearnBug screenshot here
Walkthrough

Remove 2nd node from end: 1→2→3→4→5, n=2

Target: node 4 (2nd from end). Result should be: 1→2→3→5

Init

Create dummy → head. slow = dummy, fast = dummy.

Dummy handles edge case of removing the head. slow starts at dummy — when done, slow.next is the target to remove.

dummyslow
1fast
2
3
4
5
1

Advance fast n=2 steps ahead

fast moves 2 steps: dummy→1→2. Now fast is at node 2. Gap = 2 between slow (dummy) and fast (node 2).

dummyslow
1
2fast
3
4
5
Gap = 2 nodes between slow and fast
2

Move both together until fast.next = None

Both advance in lockstep: slow→1, fast→3. Then slow→2, fast→4. Then slow→3, fast→5. fast.next = None → stop. slow is at node 3 — one before the target (node 4).

dummy
1
2
3slow
4target
5fast
3

Delete: slow.next = slow.next.next

Skip node 4 by pointing slow (node 3) directly at node 5. Node 4 is now unreachable — removed.

1
2
3
5
None

Result: 1→2→3→5 ✓ Node 4 removed

Python Code

One-pass implementation with dummy head

PythonRemove Nth Node from End — O(n) one pass
def remove_nth_from_end(head, n):
    dummy      = ListNode(0)
    dummy.next = head
    slow = fast = dummy

    # advance fast n+1 steps (so slow lands BEFORE the target)
    for _ in range(n + 1):
        fast = fast.next

    # move both until fast is None
    while fast:
        slow = slow.next
        fast = fast.next

    # skip the target node
    slow.next = slow.next.next

    return dummy.next

Time & Space Complexity

TimeO(n)One pass — fast traverses the full list, slow traverses n-N steps
SpaceO(1)Only two pointers and a dummy node — constant extra memory
vs Two-pass2 passes → 1 passCould count length then walk to position — two passes, same O(n)
Why n+1 steps?Land before targetNeed slow at node BEFORE target to do slow.next = slow.next.next

Frequently asked questions

Why advance fast n+1 steps instead of n?

We need slow to stop at the node before the target, so we can do slow.next = slow.next.next. If fast is n+1 ahead of slow, when fast reaches None, slow is at position (length - n), which is exactly one before the Nth node from the end. Advancing only n steps would land slow on the target itself — leaving no way to remove it.

Why use a dummy head?

If n equals the length of the list, the target is the head node. Without a dummy, slow would need to be before the head — impossible without a sentinel. The dummy node sits before the real head, so slow can point to it and still perform slow.next = slow.next.next to remove the head cleanly.

How do you handle the edge case where n equals the list length?

This is the head removal case. With the dummy node approach it's handled automatically: slow stays at the dummy, and slow.next = slow.next.next makes dummy point to the second node, effectively removing the head. Return dummy.next as the new head.

What is the LeetCode problem for this?

LeetCode 19 — Remove Nth Node from End of List. The expected solution is the one-pass two-pointer approach. The dummy head is strongly recommended. This problem is commonly asked as a demonstration of the fixed-gap two-pointer pattern and is a prerequisite for understanding LC 876 (Middle of List) and LC 234 (Palindrome Linked List).

Can this technique find the Nth node from the end without removing it?

Yes — just stop at the target instead of one before it. Advance fast n steps (not n+1). When fast hits None, slow is at the Nth node from the end. This is the basis for problems like "find the kth largest node" or "check if the Nth from end has value X".

Watch the gap pointers find and remove your target node

Paste your linked list and n into LearnBug and see the one-pass deletion happen step by step.

Open Playground →