LeetCode #19 Medium

Remove Nth Node From End of List

Remove the n-th node from the end of the list and return the head — ideally in one pass.

linked-listtwo-pointers
Open on LeetCode ↗
02

Intuition

💡

Counting from the end is awkward in a singly linked list. Give a fast pointer an n-node head start; when fast reaches the end, slow sits exactly on the node just before the one to remove.

03

Approach

1

Counting first means two passes

Position-from-the-end is awkward in a singly linked list because you can only move forward. The obvious fix — measure the length, then walk to length − n — works but takes two passes. We can do it in one by keeping a fixed gap between two pointers.

2

Build an n-node gap, then move together

Advance a fast pointer n steps first, so fast and slow are exactly n nodes apart. Now move both at the same speed until fast reaches the last node. Because the gap is preserved, slow ends up n nodes from the end — specifically, right before the node we want to remove. We converted 'nth from the end' into 'where fast stops,' which needs no length.

3

A dummy head handles removing the first node

If the target is the head itself, slow needs to stop 'before the head' — so we start both pointers on a dummy node placed before the head. After the walk, splice the target out with slow.next = slow.next.next and return dummy.next. The dummy makes head-removal and single-node lists work with no special case. One pass, O(L) time, O(1) space.

04

Solution & live demo

python
1class Solution:
2 def removeNthFromEnd(self, head, n):
3 dummy = ListNode(0, head)
4 slow = fast = dummy
5 for _ in range(n):
6 fast = fast.next
7 while fast.next:
8 slow = slow.next
9 fast = fast.next
10 slow.next = slow.next.next
11 return dummy.next
05

Edge cases

Removing the head (n equals length)

The dummy node lets slow stop before the head, so removing the first node needs no special case.

Single node, n = 1

After fast's head start it is null; slow stays on the dummy and unlinks the only node, returning an empty list.

Removing the tail

fast walks to the last node; slow stops one before, splicing out the final node cleanly.

06

Complexity

Time
O(L)
Space
O(1)
One pass over L nodes with two pointers.