https://leetcode.com/problems/merge-k-sorted-lists/
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.
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 list:
1->1->2->3->4->4->5->6
Example 2:
Example 3:
仿照 21. 題 方法三的解法,所有的 lists 在同一輪先找出最小值
Runtime: 4802 ms, faster than 5.01% of JavaScript online submissions for Merge k Sorted Lists.
Memory Usage: 74.4 MB, less than 5.00% of JavaScript online submissions for Merge k Sorted Lists.
Time complexity : O(kN) where k is the number of linked lists.
每次比較最小值都是 k 次,總共有 N 個 node 要比較
Space complexity : O(1)
將 lists 兩兩 套用 21. 題 方法三的解法,接著最後 merge 在一起
ex. [list1, list2, list3, list4] => [list12, list34] => [list1234]
Runtime: 144 ms, faster than 63.26% of JavaScript online submissions for Merge k Sorted Lists.
Memory Usage: 47.3 MB, less than 15.01% of JavaScript online submissions for Merge k Sorted Lists.
Time complexity : O(Nlogk) where k is the number of linked lists.
Space complexity : O(1)