Skip to content

Remove Nth Node From End of List

Solve on LeetCodeMedium
Time
Space

Given the head of a linked list, remove the nthn^{th} node from the end of the list and return its head.

Example 1:

The list 1→2→3→4→5 with node 4 removed, giving 1→2→3→5

Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

Example 2:

Input: head = [1], n = 1
Output: []

Example 3:

Input: head = [1,2], n = 1
Output: [1]

Constraints:

  • The number of nodes in the list is sz.
  • 1≤sz≤301 \le sz \le 30
  • 0≤0 \le Node.val ≤100\le 100
  • 1≤n≤sz1 \le n \le sz

Follow-up: Could you do this in one pass?

The direct method counts the length L, then walks again to node L - n. That is two passes; the follow-up asks for one.

To find the n-th node from the end without knowing L, keep two pointers exactly n nodes apart. When the front pointer fast falls off the end of the list, the back pointer slow is n nodes behind the end — which is exactly the node before the one to delete, if we start both from a dummy node placed in front of head.

The dummy node also removes the special case of deleting the head itself: the head simply becomes “the node after the dummy”, and we return dummy.next at the end.

  1. Create a dummy node whose next is head. Set fast = head and slow = &dummy.
  2. Advance fast by n nodes. Now fast is n nodes ahead of slow->next.
  3. While fast is not null, advance both fast and slow by one node. The gap is preserved.
  4. When fast becomes null, slow->next is the n-th node from the end. Save it as target, unlink it with slow->next = target->next, and free it.
  5. Return dummy.next.

D is the dummy node. Blue is slow, orange is the node fast points at; the gap between them never changes:

  1. Two pointers n = 2 apart on the list 1→2→3→4→5, step 1 of 8
  2. Two pointers n = 2 apart on the list 1→2→3→4→5, step 2 of 8
  3. Two pointers n = 2 apart on the list 1→2→3→4→5, step 3 of 8
  4. Two pointers n = 2 apart on the list 1→2→3→4→5, step 4 of 8
  5. Two pointers n = 2 apart on the list 1→2→3→4→5, step 5 of 8
  6. Two pointers n = 2 apart on the list 1→2→3→4→5, step 6 of 8
  7. Two pointers n = 2 apart on the list 1→2→3→4→5, step 7 of 8
  8. Two pointers n = 2 apart on the list 1→2→3→4→5, step 8 of 8
8 steps. Swipe or scroll to move between them.
  • Time: O(L)O(L) — fast visits every node once and slow trails behind it, all in a single pass.
  • Space: O(1)O(1) — a dummy node and two pointers.
  • Removing the head (n == sz, e.g. Example 2 or [1,2] with n = 2): after step 2 fast is already null, so slow stays at dummy and dummy.next is moved past the head. The result may be an empty list.
  • Removing the tail (n = 1, Example 3): slow stops at the second-to-last node and its next becomes null.

Think before you peek

Work out an approach on your own first — the explanation will still be here when you want it.

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(0, head);
ListNode* fast = head;
ListNode* slow = &dummy;
for (int i = 0; i < n; i++) fast = fast->next;
while (fast) {
fast = fast->next;
slow = slow->next;
}
ListNode* target = slow->next;
slow->next = target->next;
delete target;
return dummy.next;
}
};
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = head, slow = dummy;
for (int i = 0; i < n; i++) fast = fast.next;
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
class Solution:
def removeNthFromEnd(self, head: ListNode | None, n: int) -> ListNode | None:
dummy = ListNode(0, head)
fast, slow = head, dummy
for _ in range(n):
fast = fast.next
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next

Give it a try first

Attempt the problem on LeetCode — the code will still be here.