Remove Nth Node From End of List
Remove the n-th node from the end of a singly linked list in a single pass using a two-pointer gap strategy with a dummy sentinel.
01The problem
Given the head of a linked list, remove the n-th node from the end of the list and return its head.
Could you do this in one pass?
| Input | Output | Why |
|---|---|---|
[1, 2, 3, 4, 5], n = 2 | [1, 2, 3, 5] | 4 is the 2nd node from the end; it is removed |
[1], n = 1 | [] | Only node is 1st from the end; list becomes empty |
[1, 2], n = 1 | [1] | Tail node removed |
[1, 2], n = 2 | [2] | Head node removed |
Constraints: 1 to 30 nodes, node values between 0 and 100, 1 <= n <= size.
Loading the rest of the lesson…