Remove Nth Node ​
Remove the n-th node from the end of a linked list and return its head, ideally in one pass.
Approach ​
Two pass - Calculate count, go $count - n$ and delete. One pass -
- Move $n$ steps.
- Make a dummy that points to
head. Start another point from dummy, creating a window of size $n + 1$. - Move both pointers one by one.
- When right pointer reaches null, the left pointer is just behind the node to remove.
r.next = r.next.next