37. Remove Nth Node From End of List
mediumAsked at RedditRemove 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 Alex Chen, 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 <= 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: 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.
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.
Practice these live with InterviewChamp.AI
Drill Remove Nth Node From End of List and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →