#23

Merge k Sorted Lists

expert · 1200 · lc hard +32 · verified · 59% accepted · 21,192 likes · top 56%

play →

Description

Combine an array of k individually sorted linked lists into one sorted linked list and return it.

Example 1:

Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted linked list:
1->1->2->3->4->4->5->6

Example 2:

Input: lists = []
Output: []

Example 3:

Input: lists = [[]]
Output: []

Code

1
2
3
4
5
6
7
8