Top 150 InterviewEasy

Merge K Sorted Lists

Detailed guide and Python implementation for the 'Merge K Sorted Lists' problem.

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?
Consider using Linked List-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.