Merge k Sorted Lists
You are given an array lists of k linked lists. Each list is already sorted in ascending order.
Merge them into one sorted linked list and return its head. Reuse the existing nodes: relink them rather than creating new ones. If there are no nodes at all, return null.
Some lists may be empty (null), and lists itself may be empty. The tests show each list as an array of values; your function receives the head nodes.
Constraints
0 <= k <= 10^40 <=length of each list<= 500, and at most10^4nodes in total-10^4 <= node.val <= 10^4- Every list is sorted in ascending order.
Examples
Example 1
Input: lists = [[1,4,7],[2,5,8],[3,6,9]] Output: [1,2,3,4,5,6,7,8,9]
The three lists interleave perfectly: take 1, 2, 3 from their heads in turn, then 4, 5, 6, and so on.
Example 2
Input: lists = [] Output: []
No lists at all: return null, which is shown as [].
Example 3
Input: lists = [[]] Output: []
One list, and it is empty: the result is empty too.
JavaScript you'll use: Heaps and priority queues Linked list and tree nodes
/**
* Linked list nodes are provided (you don't need to define them):
* class ListNode { constructor(val = 0, next = null) { this.val = val; this.next = next; } }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
function mergeKLists(lists) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.