39. Remove Nth Node From End of List
Medium · Linked List
Given a linked list, remove the nth node from the end of the list and return the head of the modified list.
The list is 1-indexed from the end, meaning the last node is the 1st node from the end. You must solve this in one pass if possible.
Note: If removing the head node, return the new head. If the list becomes empty, return null.
Examples
Example 1 Input: list = [1, 2, 3, 4, 5], n = 2 Output: [1, 2, 3, 5] Explanation: The 2nd node from the end is node with value 4. After removing it, the list becomes [1, 2, 3, 5].
Example 2 Input: list = [1], n = 1 Output: [] Explanation: The only node is the 1st from the end. Removing it leaves an empty list.
Constraints
- Standard input/output constraints apply