Skip to main content

37. Remove Nth Node From End of List

mediumAsked at Salesforce

Remove the nth node from the end of a linked list in one pass. Salesforce uses this to test the two-pointer gap technique that avoids a second pass.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce backend phone-screen favorite for one-pass linked-list questions.
  • Blind (2025)Tests dummy-head usage.

Problem

Given the head of a linked list, remove the nth node from the end of the list and return its head. Try to do it 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 length, then traverse

Count the list length L. Walk again to position L-n; remove next.

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

Tradeoff: Two passes. Salesforce explicitly asks for one-pass.

2. Two-pointer gap of n

Use a dummy head. Advance fast pointer n+1 steps. Then move both until fast is null. slow.next is the node to remove.

Time
O(n)
Space
O(1)
function removeNthFromEnd(head, n) {
  const dummy = { next: head };
  let slow = dummy, fast = dummy;
  for (let i = 0; i <= n; 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 means removing the actual head doesn't require a special case.

Salesforce-specific tips

Salesforce uses this to test whether you handle the 'remove head' edge case via dummy head WITHOUT extra branching. Bonus signal: discuss that the gap-of-n+1 (not n) is what gets slow to point at the predecessor of the target, not the target itself.

Common mistakes

  • Advancing fast n steps instead of n+1 — leaves slow pointing AT the target, not its predecessor.
  • Not using a dummy head — removing the actual head requires a special case.
  • Forgetting to walk fast while fast.next isn't null — off by one.

Follow-up questions

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

  • Find the nth-from-end without modifying the list.
  • Reverse Linked List (LC 206).
  • Middle of the Linked List (LC 876) — uses the same two-pointer pattern.

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 dummy head?

Removing the actual first node requires modifying head. With dummy, the predecessor of the head exists explicitly, so the same code works for any position.

Why advance fast n+1 steps?

To make the gap between fast and slow equal to n+1. When fast is null (past the end), slow points at the predecessor of the target.

Practice these live with InterviewChamp.AI

Drill Remove Nth Node From End of List and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →