DSA Factory
Free Mixed sets lessonsMixed sets · Stage 4 · Set 5 · Step 3

Sort the chain

Sort a linked list with merge sort - split it at the middle, sort both halves, and merge them. About 20 minutes.

Split, sort, merge

Quicksort and heapsort need to jump to positions, which a chain does not allow. Merge sort needs only two things - splitting at the middle and merging two sorted lists - and a chain does both well.

Find the end of the first half with slow and fast pointers, cut the link after it, and sort each half by calling the same function. A list with zero or one nodes is already sorted.

Base case: zero or one node is already sorted.
Merge: the two sorted halves, as in an earlier step.
In code
if head is None or head.next is None:
    return head      # 0 or 1 node: sorted
Sort 7, 3, 9, 1, 5, 2.
7
0
3
1
9
2
1
3
5
4
2
5
leftright

Cut 7, 3, 9, 1, 5, 2 in the middle into 7, 3, 9 and 1, 5, 2.

Move 1 of 4

Cut before you recurse

After finding the middle, the second half starts at the node after slow. Save it, then set slow.next to nothing, which ends the first half there.

Starting fast one node ahead of slow makes the first half the smaller one for even lengths. That matters: for two nodes it makes the halves one node each, so the recursion always gets shorter and always stops.

Start fast one ahead: so a two-node list still splits.
Cut the first half: or the recursion never ends.
In code
second = slow.next
slow.next = None
Quick check

Why is merge sort a good fit for a linked list?

  1. AIt only needs to split at the middle and to merge, and both are cheap on a chain
  2. BIt looks up items by position quickly
  3. CIt needs no recursion
Show the answer

It only needs to split at the middle and to merge, and both are cheap on a chain. Both moves work by relinking nodes with no random access and no extra array.

Your problem

Sort a linked list

Given the head of a singly linked list, sort it in ascending order and return the head of the sorted list. Aim for n log n time.

Example
head = [4, 2, 1, 3] → [1, 2, 3, 4]

0 ≤ number of nodes ≤ 5,000 · -100,000 ≤ node.val ≤ 100,000

Solve it in your browserFree account, no card. Python, C++, Java or JavaScript, with hints if you get stuck, and your progress is saved.
Sign up free to solve