Remove Nth Node From End of List
Pattern: Fast and slow pointers
Put fast n nodes ahead of slow, both starting on a dummy. Move them together until fast is on the last node; slow is then just before the node to remove.
The problem
Given the head of a linked list and n (with 1 ≤ n ≤ length), remove the n-th node from the end and return the head.
For example, 1 → 2 → 3 → 4 → 5 with n = 2 becomes 1 → 2 → 3 → 5, and 1 with n = 1 becomes empty. LeetCode asks
for one pass.
The function name removeNthFromEnd matches LeetCode. Use the link above for the exact statement and limits.
Think first
- With the length known, which node from the front is it?
- Without the length, how can a gap between two pointers stand in for it?
- What if the node to remove is the head?
Hints
Brute force, and why it fails
Count the length, then delete node length - n + 1 from the front: two passes, O(n). The gap method does it in one
pass.
The key insight
A fixed gap between two pointers measures distance from the end without counting. The dummy node makes "remove the head" just another case.
Dry run
The list is 1 → 2 → 3 → 4 → 5 and n = 2, so the 4 must go. Both pointers start on a dummy.
The plan
dummy.next = head; slow = fast = dummy
repeat n times: fast = fast.next
while fast.next is not null: fast = fast.next; slow = slow.next
slow.next = slow.next.next
return dummy.next
Complexity
- Time
O(n)- Space
O(1)
fast walks the list once.
Edge cases
nequal to the length: the head is removed.n = 1: the tail is removed.- One node: the result is empty.
I've built my logic, show the code
Java
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode fast = dummy, slow = dummy;
for (int i = 0; i < n; i++) fast = fast.next; // fast starts n nodes ahead
while (fast.next != null) { fast = fast.next; slow = slow.next; } // move both until fast is last
slow.next = slow.next.next; // slow is just before the n-th node from the end
return dummy.next;
}
}C++
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(0);
dummy.next = head;
ListNode *fast = &dummy, *slow = &dummy;
for (int i = 0; i < n; i++) fast = fast->next; // fast starts n nodes ahead
while (fast->next != NULL) { fast = fast->next; slow = slow->next; } // move both until fast is last
slow->next = slow->next->next; // slow is just before the n-th node from the end
return dummy.next;
}
};Python
from lib import ListNode
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
dummy = ListNode(0, head)
fast = slow = dummy
for _ in range(n): # fast starts n nodes ahead
fast = fast.next
while fast.next is not None: # move both until fast is last
fast, slow = fast.next, slow.next
slow.next = slow.next.next # slow is just before the n-th node from the end
return dummy.nextCommon mistakes
- Starting at the head instead of a dummy, which fails when the head must go.
- Moving
fastn - 1orn + 1steps, which removes the wrong node. - Stopping when
fastis null instead of on the last node, which putsslowone node too far.