Practice/Meta/Leetcode 19. Remove Nth Node From End of List
CodingMust
Given the head of a singly linked list, remove the node that is k positions from the end of the list and return the modified list's head.
The position counting starts at 1, where position 1 from the end is the last node (tail), position 2 from the end is the second-to-last node, and so on.
Example 1:
Input: head = [1,2,3,4,5], k = 2 Output: [1,2,3,5] Explanation: The 2nd node from the end is the node with value 4. After removing it, the list becomes [1,2,3,5].
Example 2:
Input: head = [1], k = 1 Output: [] Explanation: The only node is removed, resulting in an empty list.
Example 3:
Input: head = [1,2], k = 1 Output: [1] Explanation: The last node (with value 2) is removed, leaving only the head node.
Example 4:
Input: head = [1,2], k = 2 Output: [2] Explanation: The head node (with value 1) is removed, and the second node becomes the new head.