Remove Nth Node From End of List
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
Given the head of a linked list, remove the n-th node from the end and return the head of the resulting list.
Understand the problem
- Count from the back:
n = 1is the last node,n = 2is the second-to-last, and so on. - Removing a node means the node before it should skip over it and point to the node after it.
nis always valid (between 1 and the length), so the node always exists.- If the removed node is the head, the new head is the second node.
Example 1
Input: head = [1, 2, 3, 4, 5], n = 2
Output: [1, 2, 3, 5]Counting from the end: 5 is 1st, 4 is 2nd. Remove 4.
Example 2
Input: head = [1], n = 1
Output: []The only node is also the last one. Removing it leaves an empty list.
Example 3
Input: head = [1, 2], n = 2
Output: [2]The 2nd from the end is the head itself, so the new head is 2.
Constraints
1 <= number of nodes (sz) <= 301 <= n <= sz
Note
Follow-up: counting the length first takes two passes. Can you do it in one?
Hints
Stuck? Open one hint at a time. Each one gives away a little more than the last.
The idea in one sentence
Move a "fast" pointer n steps ahead, then move it and a "slow" pointer together until fast hits the end: slow is now right before the node to delete.
The simple approach (two passes)
- Walk the list to count its length
L. - The node to remove is at position
L - nfrom the start (counting from 0). - Walk again to position
L - n - 1(the node before it) and skip over the target.
This is correct and O(L) time. It just walks the list twice. The follow-up asks for one pass.
The key insight
Use two pointers with a fixed gap:
- If
fastisnnodes ahead ofslow, and they move together one step at a time, the gap staysn. - When
fastreaches the last node,slowisnnodes behind it: right before then-th from the end.
To delete a node, you need the node before it, which is exactly where slow ends up.
Why a dummy node? If we have to remove the head, there's no node before it. A fake dummy node placed in front of head gives every real node a "before", including the head. Both pointers start at dummy, and we return dummy.next at the end.
Step by step
dummy → 1 → 2 → 3 → 4 → 5, n = 2:
1. Move fast ahead n = 2 steps: fast goes dummy → 1 → 2.
2. Move both until fast.next is null:
slow | fast | fast.next |
|---|---|---|
dummy | 2 | 3 |
1 | 3 | 4 |
2 | 4 | 5 |
3 | 5 | null → stop |
3. Skip the next node: slow is 3, so slow.next is 4, the 2nd from the end. Set 3.next = 5.
Result: 1 → 2 → 3 → 5 ✅
The code
The reference solution is below, with comments on every important line.
How fast is it?
- Time: O(L). One pass:
fastwalks the whole list once, andslowfollows. - Memory: O(1). Two pointers and a dummy node.
Common mistakes
- No dummy node. Then removing the head (
nequals the length) needs a special case, and forgetting it crashes or returns the wrong head. - An off-by-one gap. Starting both at
headinstead ofdummy, or stopping atfast === nullinstead offast.next === null, leavesslowon the node to delete instead of the one before it. - Returning
head. If the head was removed,headis gone. Returndummy.next.
Going deeper
The "fixed gap" trick is a cousin of the fast/slow pointers from Linked List Cycle: there, the pointers move at different speeds; here, they move at the same speed with a head start. Both let you find a position in a linked list without knowing its length.
Reference solution
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode | null}
*/
export default function removeNthFromEnd(head, n) {
// A fake node before head, so even the head has a node "before" it.
const dummy = new ListNode(0, head)
let slow = dummy
let fast = dummy
// Give fast a head start of n nodes.
for (let i = 0; i < n; i++) fast = fast.next
// Move both until fast is on the LAST node. The gap stays n,
// so slow ends up right BEFORE the n-th node from the end.
while (fast.next) {
slow = slow.next
fast = fast.next
}
// Skip over the target node.
slow.next = slow.next.next
return dummy.next // not head: the head itself may have been removed
}export default function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
// A fake node before head, so even the head has a node "before" it.
const dummy = new ListNode(0, head)
let slow = dummy
let fast = dummy
// Give fast a head start of n nodes.
for (let i = 0; i < n; i++) fast = fast.next!
// Move both until fast is on the LAST node. The gap stays n,
// so slow ends up right BEFORE the n-th node from the end.
while (fast.next) {
slow = slow.next!
fast = fast.next
}
// Skip over the target node.
slow.next = slow.next!.next
return dummy.next // not head: the head itself may have been removed
}def remove_nth_from_end(head: ListNode | None, n: int) -> ListNode | None:
# A fake node before head, so even the head has a node "before" it.
dummy = ListNode(0, head)
slow = fast = dummy
# Give fast a head start of n nodes.
for _ in range(n):
fast = fast.next
# Move both until fast is on the LAST node. The gap stays n,
# so slow ends up right BEFORE the n-th node from the end.
while fast.next:
slow = slow.next
fast = fast.next
# Skip over the target node.
slow.next = slow.next.next
return dummy.next # not head: the head itself may have been removed