Sort List
Sort a linked list in O(n log n) time and O(1) memory space using merge sort.

30:00

Sort List
medium
Topics
Companies

Sort a linked list in O(n log n) time and O(1) memory space using merge sort.

Example 1:
Input: {"head":[4,2,1,3]}
Output: [1,2,3,4]
Constraints:
  • The number of nodes is in the range [0,5×104][0, 5 \times 10^4].

  • 105Node.val105-10^5 \leq \text{Node.val} \leq 10^5

Input
arr ={"head":[4,2,1,3]}

Phase: Finding Middle (Slow/Fast Pointers)

Starting merge sort on linked list

head

4

2

1

3

NULL
head: 4
Variables
No variables to display
DepthFunction Call
Stack empty
0/12