easyLinked ListPure DSA~13 min
Trim Nth Recent Event
An append-only event chain keeps newest events at the tail. Retention policy says drop the n-th event counting back from the newest. Remove it in a single pass.
Problem
Given the head of a singly linked list, remove the n-th node from the end of the list and return the head of the modified list.
Input
The head of a singly linked list with L nodes (1 ≤ L ≤ 30) and 1 ≤ n ≤ L.
Output
The head of the list after removing the target node.
Constraints
- 1 ≤ n ≤ list length
- Do it in one pass
- Removing the head is possible when n equals the length
Examples
Example 1
Input
list = 1→2→3→4→5, n = 2
Output
1→2→3→5
The 2nd-from-end node (value 4) is removed.
Example 2
Input
list = 1, n = 1
Output
empty list
The only node is the 1st from the end.