Algo-Sim › Linked Lists
Sort List
Medium · Linked Lists · step through it in Java, Python and C++ on your own input
Merge sort on a linked list: split at the middle with slow/fast pointers, sort each half recursively, then merge the two sorted halves.
Problem
Sort the singly linked list starting at head in ascending order and return its new head.
Constraints
- 0 ≤ number of nodes ≤ 5 · 10⁴
- -10⁵ ≤ Node.val ≤ 10⁵
- Follow-up: O(n log n) time with O(1) extra space (bottom-up merge sort)
Edge cases to test
- Empty or one node
- Already sorted
- Many duplicates
Follow-up questions
- How do you get O(1) extra space?
- Bottom-up merge sort: merge runs of size 1, 2, 4… iteratively instead of recursing. O(n log n) time, O(1) space.
- Why merge sort for lists?
- Merging needs no random access and no extra arrays; quicksort’s partitioning is awkward without indices.
Solution
Java
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode mid = slow.next;
slow.next = null;
ListNode left = sortList(head);
ListNode right = sortList(mid);
ListNode dummy = new ListNode(0), tail = dummy;
while (left != null && right != null) {
if (left.val <= right.val) {
tail.next = left;
left = left.next;
} else {
tail.next = right;
right = right.next;
}
tail = tail.next;
}
tail.next = (left != null) ? left : right;
return dummy.next;
}Python
def sort_list(head):
if not head or not head.next: return head
slow, fast = head, head.next
while fast and fast.next:
slow, fast = slow.next, fast.next.next
mid, slow.next = slow.next, None
left = sort_list(head)
right = sort_list(mid)
dummy = tail = ListNode()
while left and right:
if left.val <= right.val:
tail.next, left = left, left.next
else:
tail.next, right = right, right.next
tail = tail.next
tail.next = left or right
return dummy.nextC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (head == nullptr || head->next == nullptr) return head;
ListNode *slow = head, *fast = head->next;
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
ListNode *mid = slow->next;
slow->next = nullptr;
ListNode *left = sortList(head);
ListNode *right = sortList(mid);
ListNode dummy, *tail = &dummy;
while (left != nullptr && right != nullptr) {
if (left->val <= right->val) {
tail->next = left;
left = left->next;
} else {
tail->next = right;
right = right->next;
}
tail = tail->next;
}
tail->next = (left != nullptr) ? left : right;
return dummy.next;
}
};
More Linked Lists problems
- Reverse Linked List (Easy)
- Reverse Linked List (Recursive) (Easy)
- Merge Two Sorted Lists (Easy)
- Middle of the Linked List (Easy)
- Palindrome Linked List (Easy)
- Linked List Cycle (Easy)
- Linked List Cycle II (Medium)
- Intersection of Two Linked Lists (Easy)
- Find the Duplicate Number (Medium)
- Remove Nth Node From End of List (Medium)
- Reorder List (Medium)
- Add Two Numbers (Medium)
- Copy List with Random Pointer (Medium)
- Reverse Nodes in k-Group (Hard)