Magicsheet logo

Remove Nth Node From End of List

Medium
40.3%
Updated 6/1/2025

Remove Nth Node From End of List

What is this problem about?

The Remove Nth Node From End of List interview question asks you to delete the node that is exactly n positions from the end of a singly linked list, then return the head of the modified list. The challenge is to do this in a single pass through the list, without first counting the total length.

Why is this asked in interviews?

This problem is a staple at Apple, Uber, Goldman Sachs, Microsoft, Meta, Amazon, Google, Bloomberg, and Adobe because it is the canonical demonstration of the two-pointer (fast/slow) technique applied to linked lists. The problem is simple enough to code quickly but tricky enough that candidates without the two-pointer intuition will instinctively make two passes. It also tests dummy-node usage for clean head-deletion handling.

Algorithmic pattern used

The pattern is the two-pointer linked list technique with a dummy head. Create a dummy node pointing to head. Set fast and slow both to dummy. Advance fast exactly n+1 steps. Then advance both fast and slow simultaneously until fast is null. At this point, slow is the node just before the target node. Set slow.next = slow.next.next to remove the target node. Return dummy.next.

Example explanation

List: 1 → 2 → 3 → 4 → 5, n = 2.

  • dummy → 1 → 2 → 3 → 4 → 5.
  • Advance fast n+1=3 steps: dummy(0) → 1(1) → 2(2) → 3(3). fast=3.
  • Move both until fast=null: slow=1, fast=4 → slow=2, fast=5 → slow=3, fast=null.
  • slow=3. slow.next (=4) is the 2nd from end. Set slow.next = 5.

Result: 1 → 2 → 3 → 5.

Common mistakes candidates make

  • Advancing fast exactly n steps instead of n+1, causing slow to land ON the target node rather than before it.
  • Not using a dummy node, making head deletion (when n = length) a special case.
  • Making two passes (count then traverse) when one pass is expected.
  • Off-by-one in the n+1 steps, especially when n equals the list length.

Interview preparation tip

For the Remove Nth Node From End of List coding problem, the two-pointer linked list interview pattern is the golden solution. Memorize the formula: advance fast n+1 steps, then move both pointers together. The dummy node ensures zero special-casing. Interviewers at Walmart Labs and TikTok sometimes ask "can you do it in one pass?" — the answer is yes, using exactly this technique. Trace through an example where n equals the list length to prove your dummy-node approach handles it.

Similar Questions