Sort a Linked List
You are given a singly linked list represented as an array of node values. Sort the list in ascending order and return the sorted list. The input array…
You are given a singly linked list represented as an array of node values. Sort the list in **ascending order** and return the sorted list. The input array represents the linked list in order from **head → tail**. For example, `[4, 2, 1, 3]` is 4 → 2 → 1 → 3; sorted ascending it becomes 1 → 2 → 3 → 4, returned as `[1, 2, 3, 4]`. For `[3, 1, 2, 3, 1]` the sorted list is `[1, 1, 2, 3, 3]`, with the duplicate 1s and 3s grouped together.
- `0 <= nodes.length <= 5 * 10^4` - `-10^5 <= nodes[i] <= 10^5` - The list is a proper singly linked list with no cycles. - The returned list must be finite, acyclic, and contain every input value in ascending order.
Example 1: input nodes = [4,2,1,3]; output [1,2,3,4]. The four values are reordered ascending into 1 → 2 → 3 → 4.
Example 2: input nodes = [3,1,2,3,1]; output [1,1,2,3,3]. Duplicates are kept; the two 1s and two 3s group together in ascending order.
Example 3: input nodes = [0,-1,3,-2,2]; output [-2,-1,0,2,3]. Sorting is numeric, so negatives land before zero and positives.
Category: Linked List. Topic: linked-list. Difficulty: Medium. Patterns: linked-list-traversal, divide-and-conquer.