Sort List gives you the head of a linked list and asks you to return the same list with its values arranged in ascending order, including lists that contain negative values.
Statement
Given the head of a linked list, return the list after sorting it in ascending order.
Constraints:
The number of nodes in the list is in the range [0,1000].
−103≤Node.value≤103
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Sort List
1.
What is the output if the following linked list is given as input?
357 → 98 → 12 → 788 → 45 → 121
A.
12 → 45 → 98 → 788 → 121 → 357
B.
12 → 45 → 98 → 121 → 788 → 357
C.
12 → 45 → 98 → 121 → 357 → 788
D.
12 → 98 → 45 → 121 → 357 → 788
1 / 3
Try it yourself
Implement your solution in the following coding playground.
An optimal solution to this problem runs in O(n logn) time and takes O(lgn) space.
Sort List gives you the head of a linked list and asks you to return the same list with its values arranged in ascending order, including lists that contain negative values.
Statement
Given the head of a linked list, return the list after sorting it in ascending order.
Constraints:
The number of nodes in the list is in the range [0,1000].
−103≤Node.value≤103
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Sort List
1.
What is the output if the following linked list is given as input?
357 → 98 → 12 → 788 → 45 → 121
A.
12 → 45 → 98 → 788 → 121 → 357
B.
12 → 45 → 98 → 121 → 788 → 357
C.
12 → 45 → 98 → 121 → 357 → 788
D.
12 → 98 → 45 → 121 → 357 → 788
1 / 3
Try it yourself
Implement your solution in the following coding playground.
An optimal solution to this problem runs in O(n logn) time and takes O(lgn) space.