396. Sort List in O(n log n) Time and O(1) Space

HardLinked ListMerge SortLinked List

Given the head of a linked list, sort it in ascending order in O(n log n) time using O(1) auxiliary space (bottom-up merge sort). The list is given as an array; return the sorted array.

Input: An array of node values.

Output: Array — the sorted list.

Examples

Example 1
Input: [4,2,1,3]
Output: [1,2,3,4]
Explanation: Sorted ascending.
Example 2
Input: [-1,5,3,4,0]
Output: [-1,0,3,4,5]
Explanation: Includes negatives.
Example 3
Input: []
Output: []
Explanation: Empty.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →