Skip to main content

37. Remove Nth Node From End of List

mediumAsked at Reddit

Remove the Nth-from-end node in a single pass. Reddit asks this to test the two-pointer-with-gap technique — the same windowed-walk used in their rate-limiter to expire the Nth-most-recent event.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit on-site favorite.
  • Blind (2025-11)Reddit infra-team round.

Problem

Given the head of a linked list, remove the nth node from the end of the list and return its head. Follow up: Could you do this in one pass?

Constraints

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

Examples

Example 1

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]

Approaches

1. Two-pass: count, then walk

First pass: count length. Second pass: walk to length - n - 1 and skip.

Time
O(L)
Space
O(1)
function removeNthFromEnd(head, n) {
  let length = 0;
  for (let c = head; c; c = c.next) length++;
  const dummy = { next: head };
  let cur = dummy;
  for (let i = 0; i < length - n; i++) cur = cur.next;
  cur.next = cur.next.next;
  return dummy.next;
}

Tradeoff: Two passes. Acceptable but the follow-up wants one pass.

2. Two-pointer with gap (optimal)

Advance fast n+1 steps. Then advance both fast and slow until fast falls off. slow.next is the one to remove.

Time
O(L)
Space
O(1)
function removeNthFromEnd(head, n) {
  const dummy = { next: head };
  let slow = dummy, fast = dummy;
  for (let i = 0; i < n + 1; i++) fast = fast.next;
  while (fast) { slow = slow.next; fast = fast.next; }
  slow.next = slow.next.next;
  return dummy.next;
}

Tradeoff: Single pass. The dummy head handles the 'remove head' case.

Reddit-specific tips

Reddit interviewers grade on the dummy-head trick — without it, removing the head node requires a special case. Bonus signal: connect to their rate-limiter expiring the Nth-most-recent event in a sliding window without rebuilding the structure.

Common mistakes

  • Forgetting the dummy head — crashes on remove-head case.
  • Advancing fast n times instead of n+1 (off by one).
  • Forgetting that slow should land at the node BEFORE the one to remove.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Reverse linked list in groups of k (LC 25).
  • Linked list cycle II (LC 142).
  • What if n could be larger than the list?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why n + 1 steps for fast?

Because slow starts at the dummy, the gap of n+1 ensures slow lands at the node BEFORE the target.

Why use a dummy node?

Removing the actual head requires re-pointing the head reference. Dummy makes head removal uniform with any other removal.

Companies that also ask Remove Nth Node From End of List