Sort List
Given the head of a singly linked list, sort the nodes into ascending order by value and return the head of the sorted list. You should aim for O(n log n) time.
Open official problem prompt ↗Reorder the existing nodes of a singly linked list so their values are non-decreasing, returning a new head, without copying the values into an array.
Sorting a shuffled deck by repeatedly cutting it into halves, sorting each half, and then riffle-merging the two ordered piles back into one ordered pile.
- Input
- head = [4,2,1,3]
- Output
- [1,2,3,4]
- Why
- The four node values 4, 2, 1, 3 rearranged in ascending order are 1, 2, 3, 4.
The number of nodes is in the range [0, 5 * 10^4]-10^5 <= Node.val <= 10^5Follow up: O(n log n) time and O(1) auxiliary space (excluding recursion stack)