You get the heads of two linked lists, each sorted from smallest to largest. Splice their nodes into one sorted list and return its head. No new value nodes are needed: the existing nodes are relinked.
Example: 2 → 6 → 9 and 3 → 4 → 10 become 2 → 3 → 4 → 6 → 9 → 10.
Try it yourself first: LeetCode 21 ↗. Then step through the solution below.
Copy every value of both lists into an array, sort it, and build a new list from it. That is O((n + m) log(n + m)) time and O(n + m) extra space, and it throws away the fact that both lists are already sorted.
The smallest value not yet merged is always at the front of one of the two lists. So compare just the two fronts, link the smaller one to the end of the result, and move on in that list.
A dummy node in front of the result means the first node needs no special case: there is always a tail to link to. When one list runs out, the rest of the other is already sorted and is linked on in one step.
The player runs the code on the example. Press → for each step: the lines it runs light up in the code.
On a phone, Full screen keeps the picture and the code on one screen.
class Node: def __init__(self, val, next=None): self.val, self.next = val, next def merge(a, b): dummy = tail = Node(0) while a and b: if a.val <= b.val: tail.next = a a = a.next else: tail.next = b b = b.next tail = tail.next tail.next = a or b return dummy.next
Now write it yourself on LeetCode 21 ↗, or go on: Invert Binary Tree →
a is 2 → 6 → 9, and b is 3 → 4 → 10. They should become one sorted list.dummy and tail both refer to it, and tail marks the end.a has 2, b has 3. 2 is smaller, so it comes next.dummy to 2.a on to 6, and tail to 2.a has 6, b has 3. 3 is smaller, so it comes next.b on to 4, and tail to 3.a has 6, b has 4. 4 is smaller, so it comes next.b on to 10, and tail to 4.a has 6, b has 10. 6 is smaller, so it comes next.a on to 9, and tail to 6.a has 9, b has 10. 9 is smaller, so it comes next.a past its last node, to no node (∅), and tail to 9.a is empty, so the rest of b, 10, is linked on in one step: 9 links to 10.dummy.next: 2, the head of the merged list.Note
In C++ the dummy is usually a local variable, Node dummy(0);, and tail starts as its address, &dummy: nothing is allocated, and nothing has to be deleted. In Java and Python it is a small object that the garbage collector removes.
| Approach | Time | Space |
|---|---|---|
| Brute force: copy into an array, sort, rebuild | O((n + m) log(n + m)) | O(n + m) |
| Recursion | O(n + m) | O(n + m) |
| Dummy node and a tail, one pass this page | O(n + m) | O(1) |
n and m are the lengths of the two lists.
Note
Merging can also be written as a recursion: the smaller front node, linked to the merge of everything after it. It is shorter, but every node adds a call on the stack, O(n + m) space, and in Python two lists of about a thousand nodes in all already hit the recursion limit.
tail.next becomes the other list, and that list comes back as it was.dummy.next, which is None.1 → 3 and 1 → 2: the test is <=, so the 1 from a goes first, then the 1 from b: 1 → 1 → 2 → 3.5 and 1 → 2 → 3 → 4: four turns take 1 to 4 from b; then b is empty, and the 5 is linked on in one step.The blocks below are steps of Merge Two Sorted Lists in words, and two of them do not belong. Put the right ones in order and check: your algorithm runs on the example, 2 → 6 → 9 and 3 → 4 → 10, so a mistake shows in its answer. Once it is right, the code appears beside your blocks.
Click the blocks in the order they run, or drag them into the lines.Tap the blocks in the order they run. Two of them don't belong. Each block keeps its indent: it runs inside the nearest line above it that ends in a colon.
Your algorithm
Blocks
Now write it yourself on LeetCode 21 ↗, or go on: Invert Binary Tree →
class Node: def __init__(self, val, next=None): self.val, self.next = val, next def merge(a, b): dummy = tail = Node(0) while a and b: if a.val <= b.val: tail.next = a a = a.next else: tail.next = b b = b.next tail = tail.next tail.next = a or b return dummy.nextWhat about k sorted lists? This is Merge k Sorted Lists (LeetCode 23), another Blind 75 problem. Put the front node of every list into a min-heap keyed by value. Take the smallest, link it to the result, and push its next node in its place. The heap compares values only: push each node with its value and its list's number, so that two equal values never fall back to comparing the nodes themselves. With N nodes in all, that is O(N log k) time and O(k) space for the heap.
LeetCode is a trademark of LeetCode LLC. VisiGrab is not affiliated with LeetCode or with Blind; the problems are told in our own words, with our own examples.