Given a singly linked list and an integer n, explain how you would remove the nth node from the end of the list in a single pass.
For example, given the list 1 -> 2 -> 3 -> 4 -> 5 and n = 2, you would remove node 4, resulting in: 1 -> 2 -> 3 -> 5.
Can you do this without first counting the total number of nodes? How do you handle the case where the node to remove is the head?
Explain your approach and analyze the time and space complexity.
How to approach it
- Hint 1
Think about using two pointers separated by a gap of n nodes.
- Hint 2
Advance one pointer n steps ahead first, then move both pointers together until the leading pointer reaches the end.
- Hint 3
Use a dummy node before the head to handle the edge case where the head itself needs to be removed.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.