Merge K Sorted Lists

IF
AlgoAxiomStaff Engineers
JSTS
Hard20 mins

You are given an array of k sorted linked lists. Merge all the linked lists into one sorted list and return it.

Examples

Example 1:

Input: lists = [[1,4,5],[1,3,4],[2,6]]

Output: [1,1,2,3,4,4,5,6]

Explanation: The three sorted lists merge into one sorted list.

Example 2:

Input: lists = []

Output: []

Explanation: No lists to merge.

Example 3:

Input: lists = [[]]

Output: []

Explanation: The only list is empty.

Constraints

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • lists[i] is sorted in ascending order.
Source: K-way Merge pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle