Remove Nth Node From End of List
Remove Nth Node From End of List is LeetCode 19 (Medium). Given the head of a linked list and a number n, remove the node that is n-th counting from the end (the last node is 1st from the end) and return the head.
nis always valid: it is between 1 and the number of nodes, so the node to remove exists.- When
nequals the length, the node to remove is the head. - The follow-up asks for one pass over the list.
- The number of nodes in the list is sz.
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
Intuition
A singly linked list cannot be read backwards, but a distance can be measured forwards. Two pointers kept a fixed number of links apart stay that far apart all the way to the end. If the front pointer reaches the end (null) while the back pointer is n + 1 links behind it, the back pointer is standing on the node just before the one to delete.
Starting both pointers on a dummy node in front of the head means that "node before the target" always exists, even when the target is the head.
"k-th from the end" in a singly linked list, with a one-pass follow-up, is the fixed-gap two-pointer pattern.
Approach
Before reading on, decide how many links ahead the front pointer must start so the back pointer stops on the node before the target, not on the target. Check your count on a one-node list with n = 1. Aim for one pass and O(1) extra space.
Two ways to find the node
Two pointers n + 1 links apart walk to the end together.
- One pass: answers the follow-up.
- No length needed: works on a stream you can read only once.
The version interviewers expect.
Count the nodes, then stop sz − n steps after the dummy.
- Easiest to reason about: plain arithmetic on positions.
- Two passes: reads the list twice.
A good first answer before the follow-up.
Both are linear with constant memory. The gap version reads each node once and is the one traced below; the counting version follows the demo.
Put both pointers on a dummy node
Create dummy with dummy.next = head, and set fast and slow to it. The dummy is the node before the head, which is exactly where slow must stop when the head is the one removed.
Open a gap of n + 1 links
Move fast forward n + 1 times. Now fast is n + 1 links ahead of slow; it may already be null when n is the length of the list.
Walk both until fast is null
Step fast and slow together until fast is null. The gap never changes, so slow ends n + 1 links before the end of the list: one node before the n-th node from the end.
Unlink the target
Set slow.next = slow.next.next and return dummy.next. Returning the dummy's next, not head, gives the right answer when the old head was removed.
Code in Python, C++ and Java
Count the length, then walk
The first pass counts the nodes, sz. The node to delete is then number sz − n + 1 from the front, so the node before it is sz − n steps after a dummy placed in front of the head. The dummy again covers the case n = sz.
Common pitfalls
Opening a gap of n instead of n + 1
for _ in range(n):
fast = fast.nextfor _ in range(n + 1):
fast = fast.nextWith a gap of n, slow stops on the target itself, and slow.next = slow.next.next removes the node after it. For [1, 2, 3, 4, 5] with n = 2 that deletes 5 instead of 4.
Starting the pointers on head instead of a dummy
fast = slow = head
dummy = ListNode(0, head) fast = slow = dummy
For [1, 2] with n = 2 the head must go, but no node sits before it. Moving fast three links from the head runs off the end and crashes on a null pointer.
Edge cases
fast moves dummy → 1 → null, the walk loop never runs, and slow (the dummy) unlinks the only node. The answer is an empty list.
The last node is removed. slow stops on the second-to-last node and its link becomes null.
Complexity
fast moves sz + 1 times in total and slow fewer, so each node is read once by each pointer. Only the dummy and two pointers are extra.Remove Nth Node From End of List FAQ
Does the one-pass version actually do less work?
Not much: two pointers each walk most of the list, about as many steps as counting and walking again. What it saves is a second read of the list, which matters only when the list can be read once.
Can recursion find the n-th node from the end?
Yes. Recurse to the end, and on the way back return how many nodes lie after the current one. The node that sees exactly n nodes after itself unlinks its next; start the recursion from a dummy so the head case works. It is one pass too, but it uses a stack frame per node.