Remove Nth Node From End
Given the head of a singly linked list, remove the nth node counting from the end of the list and return the head of the modified list.
Open official problem prompt ↗Delete a single node identified by its distance from the tail, in one traversal, without knowing the list length in advance.
Imagine two people walking a rope hand-over-hand, one starting n knots ahead. When the leader reaches the frayed end, the follower is standing exactly at the knot to cut.
- Input
- head = [1,2,3,4,5], n = 2
- Output
- [1,2,3,5]
- Why
- The 2nd node from the end is the value 4, so removing it leaves 1 -> 2 -> 3 -> 5.
The number of nodes is sz1 <= sz <= 300 <= Node.val <= 1001 <= n <= sz