Given the head of a singly linked list, sort its nodes in non-decreasing order of value and return the new head. Rearrange the existing nodes rather than creating new ones or sorting a copy of the values. One test checks this: original_positions(values) builds a list from values, runs your sort_nodes and returns, for each node of the result, the 0-based position that node had in the original list (-1 for a new node), so original_positions([30, 10, 20]) must be [1, 2, 0].
Examples
Input: head = 7 -> 3 -> 9 -> 1
Output: 1 -> 3 -> 7 -> 9
Input: head = 0 -> -6 -> 8 -> -6 -> 2
Output: -6 -> -6 -> 0 -> 2 -> 8
Constraints
- Target: O(n log n) time; do not copy the values into a Python list and call sorted()
Goals
- Apply divide and conquer directly on nodes
- Merge two sorted chains by relinking nodes