 All Problems
Merge K Sorted Lists
hard
linked list
divide and conquer
heap
merge sort
amazon
google
microsoft
facebook

You are given an array of k linked-lists, each linked-list is sorted in ascending order.

Merge all the linked-lists into one sorted linked-list and return it.

Example 1:

Input:
3
1 4 5
1 3 4
2 6
Output: 1 1 2 3 4 4 5 6

Example 2:

Input:
0

Output: (empty)

Example 3:

Input:
1
(empty)
Output: (empty)

Constraints:

  • k == lists.length
  • 0 ≤ k ≤ 10⁴
  • 0 ≤ lists[i].length ≤ 500
  • -10⁴ ≤ lists[i][j] ≤ 10⁴
  • lists[i] is sorted in ascending order
  • The sum of lists[i].length will not exceed 10⁴

Input format: First line: k (number of lists). Then k lines, each with space-separated integers for that sorted list (empty line for empty list).

Output format: Space-separated integers of the merged sorted list, or empty line if result is empty.

Run to check your code against the sample cases, or submit to run every case