Back to Practice Dashboard
Top 150 InterviewEasy
Merge K Sorted Lists
Learn how to solve the 'Merge K Sorted Lists' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
The linked lists are represented as a list of Python lists. Implement a function mergeKLists(lists: list) -> list that returns the merged sorted list.
Constraints
- •k == lists.length
- •0 <= k <= 10000
- •0 <= lists[i].length <= 500
- •-10000 <= lists[i][j] <= 10000
- •lists[i] is sorted in ascending order
- •The sum of lists[i].length will not exceed 10000
Examples
Example 1
Input
[[1,4,5],[1,3,4],[2,6]]
Output
[1,1,2,3,4,4,5,6]
Explanation
Merging [1,4,5], [1,3,4], and [2,6] gives [1,1,2,3,4,4,5,6].
Example 2
Input
[]
Output
[]
Explanation
No lists to merge, so the result is empty.
Example 3
Input
[[]]
Output
[]
Explanation
One empty list results in an empty merged list.
Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.