Mid
Linked Lists
DSA & Coding Interviews
How do you remove the Nth node from the end of a linked list?
Short answer: Two pointers with a gap of n. Advance fast n steps, then move both until fast hits the end; slow.next is the node to delete. One pass, O(1) space.
Edge cases to mention
- Remove head (n == length)
- Single node list
- n = 1 (remove tail)
Use a dummy head so deleting the real head is uniform.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png