Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to sort a linked list in O(n log n) time using constant space complexity

Hard

Problem 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; } }
Log in or sign up to run or submit your code.Log inSign up
Input
Linked list: 4 → 2 → 1 → 3
Expected Output
1 2 3 4