15% off all plans & roadmaps Use code
See plansLeetCode 23 · Solution & editorial
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge k Sorted Lists is a Heap (Priority Queue) Patterns problem, in the K-way Merge subpattern. The pattern covers top K elements, K-way merge, two heaps for median, and scheduling
Master K-way merge pattern using min heap. Merge k sorted lists and find kth smallest in matrix.
The reference solution runs in O(N log k) time and O(k) space.
Merge k Sorted Lists has been reported in interviews at Amazon, Facebook, Microsoft, Bloomberg, Apple, Oracle, Databricks, Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Merge k Sorted Lists is a Heap (Priority Queue) Patterns problem, in the K-way Merge subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.