Sort a singly linked list in ascending order with better-than-quadratic performance.
Given the head of a singly linked list, sort the list in ascending order and return the head of the sorted list.
You should aim for an efficient solution that performs well on large lists. Since the list is singly linked, a solution that repeatedly inserts or searches linearly at every step is typically too slow.
Return the head of the list after sorting all node values in nondecreasing order.
head.Example 1
Input
head = [4,2,1,3]
Output
[1,2,3,4]
Explanation
The nodes are reordered so that the values appear in ascending order.
Example 2
Input
head = [-1,5,3,4,0]
Output
[-1,0,3,4,5]
Explanation
After sorting, the linked list is arranged from smallest to largest value.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.