37. Remove Nth Node From End of List
mediumAsked at VercelRemove the nth node from the end of a linked list in one pass. Vercel asks this for the two-pointer offset trick — same pattern as their 'replay last N events' streaming primitive.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel screen; one-pass with dummy head expected.
- Blind (2026-Q1)— Listed in Vercel runtime engineer screen.
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 <= 300 <= Node.val <= 1001 <= n <= sz
Examples
Example 1
head = [1,2,3,4,5], n = 2[1,2,3,5]Example 2
head = [1], n = 1[]Approaches
1. Two-pass with length count
Pass 1 counts the length L. Pass 2 walks L - n - 1 nodes and removes the next.
- Time
- O(L)
- Space
- O(1)
function removeNthFromEnd(head, n) {
let len = 0;
for (let cur = head; cur; cur = cur.next) len++;
const dummy = { next: head };
let prev = dummy;
for (let i = 0; i < len - n; i++) prev = prev.next;
prev.next = prev.next.next;
return dummy.next;
}Tradeoff: Two passes. Vercel will ask for the one-pass version.
2. One-pass two-pointer with offset (optimal)
Advance fast by n. Then advance fast and slow together until fast.next is null. Slow now points to the node before 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; i++) fast = fast.next;
while (fast.next) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;
}Tradeoff: Single pass. The dummy head simplifies the case where the head itself is removed. The 'fast.next is null' termination puts slow exactly before the target.
Vercel-specific tips
Vercel grades the one-pass version with the dummy head. Bonus signal: explaining why dummy is needed (handles removal of the head itself without a special case) and the off-by-one when advancing fast (advance n times so the gap is n nodes).
Common mistakes
- Forgetting the dummy head — removing the first node leaves slow pointing at nothing.
- Advancing fast n+1 times — off-by-one, slow ends up at the node to remove, not before it.
- Looping until fast is null instead of fast.next — slow advances one too many.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Remove the nth node from the start — trivial.
- Remove every other node.
- What if the list is doubly linked? O(1) removal once you find the node.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why advance fast n times, not n+1?
We want slow to land at the node BEFORE the one to remove. With both starting at dummy and fast leading by n, when fast.next is null, slow.next is the target. Advancing n+1 would put slow on the target itself.
Why use a dummy head?
If n equals the list length, we remove the original head. Without dummy, slow would have nowhere to be 'before.' Dummy provides that safe predecessor.
Practice these live with InterviewChamp.AI
Drill Remove Nth Node From End of List and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →