forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0023-merge-k-sorted-lists.js
43 lines (40 loc) · 995 Bytes
/
0023-merge-k-sorted-lists.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 23. Merge k Sorted Lists
* https://leetcode.com/problems/merge-k-sorted-lists/
* Difficulty: Hard
*
* 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.
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function(lists) {
const map = new Map();
for (let list of lists) {
while (list) {
map.set(list.val, (map.get(list.val) || 0) + 1);
list = list.next;
}
}
const sorted = [...map].sort(([a], [b]) => a - b);
const result = new ListNode();
let tail = result;
for (let [key, value] of sorted) {
while (value--) {
tail.next = new ListNode(key);
tail = tail.next;
}
}
return result.next;
};