mediumLinked ListAmazonMeta
Dropkick From Tail
A queue of waiting tickets is linked front-to-back. Remove the nth-from-last ticket in one pass.
Given the head of a linked list and integer n, remove the nth node from the end and return the (possibly new) head.
Choose approach
1
2
3
4
5
nil
fastslow
1/7
Narrationstep 1 of 7
Two cursors from a dummy head. Advance fast 2 steps so there's exactly 2 nodes between fast and slow.
Variables
n=2
How to think about it
- The gap between slow and fast is the invariant: n nodes.
- A dummy head unifies the "remove head" case.
- Walk the pair together until fast.next is null.
- Splice: slow.next = slow.next.next.
Edge cases to catch in interview
- Removing the head → dummy helps.
- Single node → removing it yields empty.
- n equal to length — removes head.
Examples
Example 1
[1,2,3,5]{"values":[1,2,3,4,5],"n":2}
Example 2
[]{"values":[1],"n":1}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed