37. Remove Nth Node From End of List
mediumAsked at DatadogRemove the Nth-from-end node in a singly linked list in one pass. Datadog tests the two-pointer-offset trick — the same pattern they use to maintain a fixed-lag window over a streaming sequence.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Datadog loops.
- Glassdoor (2026-Q1)— Datadog phone screen linked-list question.
- LeetCode Discuss (2025-09)— Datadog tagged set.
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 <= 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 passes: count length, then remove
First pass counts; second pass walks to position length - n.
- Time
- O(n)
- Space
- O(1)
function removeNthFromEnd(head, n) {
let len = 0;
for (let c = head; c; c = c.next) len++;
const dummy = { val: 0, 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. Correct but Datadog asks for one pass.
2. Two-pointer with N-step lead (optimal)
Fast pointer advances N+1 steps first. Then advance both in lockstep until fast hits null. Slow now points to the node BEFORE the one to remove.
- 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. The dummy head handles the 'remove the head itself' edge case cleanly. Datadog-canonical pattern.
Datadog-specific tips
Datadog grades on whether you use a dummy head and the fixed-lag trick. The two-pointer offset (fast leads by n+1) is non-obvious — articulate WHY the offset is n+1 (so slow ends up at the node BEFORE the target) before coding.
Common mistakes
- Forgetting the dummy head — fails when removing the actual head.
- Off-by-one in the lead step — n vs n+1. The correct offset puts slow at the predecessor.
- Manipulating head directly without a dummy — needs special case for n == length.
Follow-up questions
An interviewer at Datadog may pivot to one of these next:
- Delete Middle of Linked List (LC 2095) — same fixed-lag trick with lag = length/2.
- Reverse Last K Nodes — sliding window variant.
- Datadog-style: maintain a fixed-lag projection over an unbounded metric stream.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why dummy head?
It removes the special case for removing the actual head. With dummy.next = head and slow starting at dummy, the same code works whether you remove the head or any other node.
Why offset by n+1 instead of n?
Slow needs to stop at the PREDECESSOR of the target. If fast leads by n+1, when fast hits null, slow is exactly at predecessor.
Practice these live with InterviewChamp.AI
Drill Remove Nth Node From End of List and other Datadog interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →