Remove the Nth Node from the End of a Linked List
You are given a singly linked list represented as an array of node values and an integer k. Remove the node that sits k positions from the end of the list and…
You are given a singly linked list represented as an array of node values and an integer `k`. Remove the node that sits `k` positions from the **end** of the list and return the resulting list. We use **0-based distance from the tail**: `k = 0` removes the **last** node, `k = 1` removes the second-to-last node, and so on. The input array represents the linked list in order from **head → tail**. For example, for `[1, 2, 3, 4, 5]` (which is 1 → 2 → 3 → 4 → 5): `k = 0` removes the last node (value 5) → `[1, 2, 3, 4]`; `k = 1` removes the node at value 4 → `[1, 2, 3, 5]`; `k = 4` removes the head (value 1) → `[2, 3, 4, 5]`. Removing the only node of a single-node list yields `[]`. The input encodes BOTH the list and `k` on two lines: line 0 is the list and line 1 is the integer `k`.
- `1 <= nodes.length <= 10^5` - `0 <= k < nodes.length` - `-10^9 <= nodes[i] <= 10^9` - The list is a proper singly linked list with no cycles.
Example 1: input nodes = [1,2,3,4,5], k = 0; output [1,2,3,4]. k = 0 is the 0-based distance from the tail, so it removes the last node (value 5).
Example 2: input nodes = [1,2,3,4,5], k = 1; output [1,2,3,5]. k = 1 removes the second-to-last node — the node with value 4.
Example 3: input nodes = [7], k = 0; output []. Removing the only node leaves an empty list.
Category: Linked List. Topic: linked-list. Difficulty: Medium. Patterns: linked-list-traversal, fast-slow-pointers.