Write a function to sort a linked list in O(n log n) time using constant space complexity
HardProblem Statement
Write a function to sort a linked list in O(n log n) time using constant space complexity
Sample Input
Linked list: 4 → 2 → 1 → 3
Sample Output
1 2 3 4
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Main {
public static ListNode sortList() {
// Create the linked list here
// Your code here
return null;
}
}
Input
Linked list: 4 → 2 → 1 → 3
Expected Output
1 2 3 4