Insertion Sort List
Insertion Sort List is LeetCode 147 (Medium). Given the head of a singly linked list, sort it in ascending order using insertion sort and return the new head.
- Insertion sort keeps a sorted part at the front and moves each following item into its correct place in that part.
- On a linked list, moving an item means changing
nextpointers, not shifting values. - The list holds 1 to 5000 nodes, so the O(n²) worst case is fine here.
- The number of nodes in the list is in the range [1, 5000]
- -5000 <= Node.val <= 5000
Intuition
Split the list in two: a sorted front part and the nodes still waiting behind it. Look at the first waiting node, cur.
- No move needed: if
curis at least as large as the last sorted node, it is already in place. The sorted part just grows by one. - Move it: otherwise, unhook
curand walk the sorted part from the start until the next node is larger thancur, then hook it in there.
A dummy node before the head makes "insert at the very front" the same as any other insert, so the head needs no special case. The dummy's next is the answer.
The problem names the algorithm. The linked-list part is the work: you cannot step backward, so each search for a slot starts from the front.
Approach
Before reading on, sort 4 → 2 → 1 → 3 on paper. For each node, write which pointer you change and which node you start searching from. How many next pointers does one move change?
Put a dummy node in front
Create dummy with dummy.next = head, and let last = head be the end of the sorted part. With the dummy, a node that belongs first is placed after dummy like any other.
Leave nodes that are already in order
Take cur = last.next. If last.val <= cur.val, cur is not smaller than anything sorted, so just set last = cur. This check makes an already sorted list cost only one pass.
Find the slot from the front
Otherwise start prev at dummy and step while prev.next.val <= cur.val. The walk stops before the first node larger than cur, so equal values keep their original order. It always stops inside the sorted part, because last is larger than cur.
Relink three pointers
Unhook cur with last.next = cur.next, then hook it in with cur.next = prev.next and prev.next = cur. Do it in this order, so the rest of the list is never cut off.
Continue from the end of the sorted part
The next node to place is always last.next, whether cur moved or not. When it is None, every node is sorted; return dummy.next.
Code in Python, C++ and Java
Common pitfalls
Hooking the node in before unhooking it
cur.next = prev.next prev.next = cur last.next = cur.next
last.next = cur.next cur.next = prev.next prev.next = cur
After the first two lines cur.next points into the sorted part, so the third line makes last point back into the list and cuts off everything after cur. On 2 → 1 → 3, the node holding 2 ends up pointing to itself: 3 is lost and the loop never ends.
Returning the old head
return head
return dummy.next
The first node can move. On 4 → 2 → 1 → 3, head is still the node holding 4, which is now last, so returning it gives just 4.
Edge cases
1 → 2 → 3Every node passes the tail check, so no search runs and the sort takes O(n).
3 → 2 → 1Every node belongs at the front, so each search stops at once at dummy. Reverse order is not the slow case here; the slow case is when each node belongs near the end of the sorted part.
Complexity
Insertion Sort List FAQ
Why not use merge sort?
Merge sort sorts a linked list in O(n log n) and is the right tool when only speed matters; that is Sort List (LeetCode 148). This problem asks for insertion sort specifically, and with 5000 nodes the quadratic version is fast enough.
Is it all right to copy the values into an array, sort, and write them back?
It returns a sorted list, but it is not insertion sort and uses O(n) extra memory. Interviewers asking this problem want to see the pointer work, so moving nodes is the expected answer.
Can the search start at the last insert spot instead of the front?
Yes, if the node to place is larger than the previous insert spot, the walk can start there, and that saves steps on nearly sorted input. It does not change the O(n²) worst case, and a singly linked list still forces a restart from dummy for smaller values.