You are given an array of k linked lists lists, where each list is sorted in ascending order.
Return the sorted linked list that is the result of merging all of the individual linked lists.
Example 1:
Input: lists = [[1,2,4],[1,3,5],[3,6]]
Output: [1,1,2,3,3,4,5,6]
Example 2:
Input: lists = []
Output: []
Example 3:
Input: lists = [[]]
Output: []
Constraints:
0 <= lists.length <= 10000 <= lists[i].length <= 100-1000 <= lists[i][j] <= 1000
You should aim for a solution as good or better than O(n * k) time and O(1) space, where k is the total number of lists and n is the total number of nodes across all lists.
A brute-force solution would involve storing all n nodes in an array, sorting them, and converting the array back into a linked list, resulting in an O(nlogn) time complexity. Can you think of a better way? Perhaps consider leveraging the idea behind merging two sorted linked lists.
We can merge two sorted linked lists without using any extra space. To handle k sorted linked lists, we can iteratively merge each linked list with a resultant merged list. How can you implement this?
We iterate through the list array with index i, starting at i = 1. We merge the linked lists using mergeTwoLists(lists[i], lists[i - 1]), which returns the head of the merged list. This head is stored in lists[i], and the process continues. Finally, the merged list is obtained at the last index, and we return its head.
{
"lists": [
[
1,
2,
4
],
[
1,
3,
5
],
[
3,
6
]
]
}[
1,
1,
2,
3,
3,
4,
5,
6
]{
"lists": [
[
5,
385
],
[
-693,
-216,
862
]
]
}[
-693,
-216,
5,
385,
862
]{
"lists": [
[
-474,
237
],
[
601
],
[
512
],
[
-338,
-253,
197,
199,
284
]
]
}[
-474,
-338,
-253,
197,
199,
237,
284,
512,
601
]Sign in to Run Code and Submit