Remove Nth Node From End of List

medium

Given the head of a singly linked list, remove the nth node counting from the end of the list, then return the head of the modified list.

The list is counted 1-indexed from the tail: n = 1 is the last node, and n equal to the list's length removes the first node. After the removal, the surviving nodes keep their original order.

Hints

You need the node that is nth from the end, but you naturally walk a linked list from the front. Can you turn "from the end" into something you can measure while moving forward?
If you knew the total length L, the node to remove would be the (L − n + 1)th from the front. Can you find L in one pass — or avoid needing it at all?
Keep two pointers exactly n nodes apart. When the leader falls off the end, the trailer sits right before your target. A dummy node before the head makes removing the first node painless.

Common doubts

That's exactly why we place a dummy sentinel node before the head. The follower pointer starts on the dummy, so even when the target is the original head it still has a valid predecessor to relink.
From 1 — n = 1 is the last node and n = sz is the first node. The problem guarantees 1 <= n <= sz, so you never over-run the list.
No — both are O(L). The one-pass version just walks the list a single time instead of twice; the asymptotic cost is identical, but it satisfies the classic "can you do it in one pass?" follow-up.

Interview follow-ups

Yes — the two-pointer gap technique walks the list a single time by keeping fast and slow exactly n nodes apart.
Simpler: from a dummy, step forward n − 1 times to reach the predecessor, then unlink. No gap trick is needed since you're already counting from the front.
Add a guard: after advancing fast by n steps, if fast is already null the list is shorter than n — clarify with the interviewer whether to return the list unchanged or raise an error.

Fun facts

  • The "two pointers a fixed distance apart" idea powers a whole family of single-sweep list tricks — finding the middle, detecting a cycle, and locating a cycle's start all lean on a gap or a speed difference to expose structure in one pass.
  • A sentinel/dummy node is a classic way to delete edge cases: by guaranteeing every real node has a predecessor, head-removal simply stops being a special case.

Asked at

AmazonMicrosoftGoogleFacebookAdobe
Frequently Sometimes Occasionally
Example 1
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
The 2nd node from the end is 4 (5 is 1st, 4 is 2nd). Removing it leaves [1,2,3,5].
Example 2
Input: head = [1], n = 1
Output: []
The only node is both the last and the first — removing it empties the list.
Example 3
Input: head = [1,2], n = 1
Output: [1]
The last node (2) is removed, leaving [1].
Constraints

- The number of nodes in the list is sz. - 1 <= sz <= 30 - 0 <= Node.val <= 100 - 1 <= n <= sz

Solve this problem →