Sort List

Sort a linked list in ascending order using constant space and O(n log n) time. Example: Input: head = [4,2,1,3] Output: [1,2,3,4] Explanation: The list is sorted via merge sort or similar. Pattern focus: Merge sort on linked lists (merging sorted sublists).

Input Format

head = ListNode

Output Format

return ListNode

Constraints

  • The number of nodes is in the range [0, 5*10^4].
  • -10^5 <= Node.val <= 10^5

Examples

Example 1:

Input:

head = [4,2,1,3]

Output:

[1,2,3,4]

Explanation:

Sorted ascending.

Loading...
Sort List - Linked List DSA Problem