37. Remove Nth Node From End of List
mediumAsked at SnowflakeRemove the n-th node from the end of a linked list in one pass. Snowflake asks this to test the two-pointer-with-gap technique — the same approach used to maintain windowed iterators over streaming row sets.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake execution-engine team uses this to lead into window-iterator follow-up.
- LeetCode Discuss (2025-09)— Reported at Snowflake new-grad screens.
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[]Example 3
head = [1,2], n = 1[1]Approaches
1. Two-pass: count then walk
First pass counts length. Second pass walks to (length - n)th node.
- Time
- O(n)
- Space
- O(1)
function removeNthFromEnd(head, n) {
let length = 0;
let curr = head;
while (curr) { length++; curr = curr.next; }
const dummy = { val: 0, next: head };
curr = dummy;
for (let i = 0; i < length - n; i++) curr = curr.next;
curr.next = curr.next.next;
return dummy.next;
}Tradeoff: Two passes. Works but doesn't satisfy the one-pass follow-up.
2. Two-pointer with n-gap (optimal)
Move fast pointer n steps ahead. Then advance both until fast hits the end. Slow is now at the node before the target.
- Time
- O(n)
- Space
- O(1)
function removeNthFromEnd(head, n) {
const dummy = { val: 0, 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. Dummy head simplifies the case where the head itself is removed.
Snowflake-specific tips
Snowflake interviewers want the dummy-head pattern explicitly — it eliminates the special case for n == length. Bonus signal: connect to streaming-window iterators where you maintain a gap of N between read and emit cursors to support 'preceding N rows' window functions.
Common mistakes
- Advancing fast n times instead of n+1 — slow ends up at the target instead of one before.
- Forgetting dummy head — breaks for the case where head must be removed.
- Returning head instead of dummy.next — head may be the removed node.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Remove the first n nodes (trivial — just advance head n times).
- Remove nodes with values divisible by k.
- How does Snowflake implement ROWS N PRECEDING window?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why a gap of n+1 (looping i <= n)?
The dummy starts one before head. To leave slow one before the target, fast needs to start n+1 steps ahead — which is i <= n iterations (n+1 advances).
Why dummy head?
When n equals the list length, the head node is removed. Without a dummy, you need a special branch; with it, the removal logic is uniform.
Practice these live with InterviewChamp.AI
Drill Remove Nth Node From End of List and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →