Merge K Sorted Lists
Difficulty: Hard
Given an array of k linked list heads, each list sorted in increasing order, merge them into one sorted list and return its head.
Examples
Input: lists = [[1, 4, 5], [1, 3, 4], [2, 6]]
Output: [1, 1, 2, 3, 4, 4, 5, 6]
Input: lists = []
Output: []
Input: lists = [[]]
Output: []
The array contains one list, and that list happens to be empty.
Constraints
0 <= k <= 10^4
0 <= length of each list <= 500
The total number of nodes across all lists is at most 10^4.
Each individual list is sorted in non-decreasing order.
Approach
The most direct approach ignores that the lists are sorted: put every value in one array, sort it, and rebuild a list.
A better approach reuses the two-list merge: merge the first list with the second, then that result with the third, and so on. It avoids a full sort, but every merge re-scans the growing result, so the work scales with both the number of lists and the total number of nodes.
The efficient approach merges in pairs: round 1 merges list 1 with list 2, list 3 with list 4, and so on, roughly halving the number of lists. Repeating this takes only about log2(k) rounds, and each node is part of one two-list merge per round instead of being re-scanned by every merge.
Solutions
Brute Force — Collect All Values, Sort, Rebuild
Push every value from every list into one array, sort it, and build a new list. This works for any input order, but pays for a full sort even though the lists were already sorted.
class ListNode {
constructor(val, next = null) {
this.val = val;
this.next = next;
}
}
function mergeKLists(lists) {
const values = [];
for (const list of lists) {
let node = list;
while (node) {
values.push(node.val);
node = node.next;
}
}
values.sort((a, b) => a - b);
const dummy = new ListNode(0);
let curr = dummy;
for (const value of values) {
curr.next = new ListNode(value);
curr = curr.next;
}
return dummy.next;
}Time: O(N log N), where N is the total number of nodes across all lists · Space: O(N)
Sequential Merge — Fold Lists One at a Time
Use the two-list merge as a building block. Start with an empty result and merge in each list one after another until all are folded in.
class ListNode {
constructor(val, next = null) {
this.val = val;
this.next = next;
}
}
function mergeTwoLists(a, b) {
const dummy = new ListNode(0);
let curr = dummy;
while (a !== null && b !== null) {
if (a.val <= b.val) {
curr.next = a;
a = a.next;
} else {
curr.next = b;
b = b.next;
}
curr = curr.next;
}
curr.next = a !== null ? a : b;
return dummy.next;
}
function mergeKLists(lists) {
let result = null;
for (const list of lists) {
result = mergeTwoLists(result, list);
}
return result;
}Time: O(k * N), where k is the number of lists and N is the total number of nodes · Space: O(1) extra, reusing existing nodes
Optimal — Pairwise (Divide and Conquer) Merge
Use the same two-list merge, but merge in pairs each round: list 1 with list 2, list 3 with list 4, and so on, leaving about half as many lists. Repeat until one list remains.
The number of lists halves each round, so it takes about log2(k) rounds. Each round does O(N) work in total (every node is touched once per round), giving O(N log k) overall instead of O(N * k).
class ListNode {
constructor(val, next = null) {
this.val = val;
this.next = next;
}
}
function mergeTwoLists(a, b) {
const dummy = new ListNode(0);
let curr = dummy;
while (a !== null && b !== null) {
if (a.val <= b.val) {
curr.next = a;
a = a.next;
} else {
curr.next = b;
b = b.next;
}
curr = curr.next;
}
curr.next = a !== null ? a : b;
return dummy.next;
}
function mergeKLists(lists) {
if (lists.length === 0) return null;
let remaining = lists;
while (remaining.length > 1) {
const merged = [];
for (let i = 0; i < remaining.length; i += 2) {
const first = remaining[i];
const second = i + 1 < remaining.length ? remaining[i + 1] : null;
merged.push(mergeTwoLists(first, second));
}
remaining = merged;
}
return remaining[0];
}Time: O(N log k), where N is the total number of nodes and k is the number of lists · Space: O(k) for the array of intermediate list heads (O(1) beyond that, reusing existing nodes)