Hardware interview practice
Remove the nth node from the end
Remove the nth node from the end of a singly linked list in one traversal and return the head.
Reviewed example
Work through one case
Input
head = 1 -> 2 -> 3 -> 4 -> 5, n = 2Expected output
1 -> 2 -> 3 -> 5Keeping the lead pointer two nodes ahead places the trailing pointer immediately before node 4.
What to cover
Requirements
- Treat n as one-based from the tail.
- Leave the list unchanged when n is not positive or exceeds the list length.
- Correctly remove the head when n equals the list length.
