Remove nth Node from End of List

Drop the nth-from-last node in one pass using a fast/slow pointer gap of n.

Medium
Time O(L) Space O(1)
LeetCode
3 min read
two-pointers linked-list

Problem#

Remove the nth node from the end of a singly linked list and return the head.

Examples#

  • head = [1,2,3,4,5], n = 2[1,2,3,5]
  • head = [1], n = 1[]
  • head = [1,2], n = 1[1]

Constraints#

  • List length in [1, 30], 1 <= n <= length.

Hints#

Hint 1
Two passes works (count then delete). Can you do it in one?
Hint 2
Use a dummy node so removing the head is no special case.

Approach#

Two passes — count length, walk again to length - n. Clear but two traversals.
Gap of n — advance fast by n first, then move slow and fast together until fast reaches the end. slow stops at the predecessor of the target.

Solution#

Remove nth Node from End
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(0, head);
ListNode *slow = &dummy, *fast = &dummy;
for (int i = 0; i < n; ++i) fast = fast->next;
while (fast->next) {
slow = slow->next;
fast = fast->next;
}
ListNode* del = slow->next;
slow->next = del->next;
delete del;
return dummy.next;
}
};

Editorial#

The dummy node converts “remove the head” into “remove the second node” — the pointer chain stays uniform. Starting both pointers at the dummy and advancing fast by n creates a permanent n-node gap. When fast->next becomes null, slow sits exactly at the predecessor of the node to delete (because the gap is n, and the last node is at position length, so slow is at length - n, the predecessor).

Complexity#

  • Time: O(L) — one pass.
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.