-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeKSortedLL.js
44 lines (38 loc) · 981 Bytes
/
mergeKSortedLL.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
44
// 23. Merge k Sorted Lists
// https://leetcode.com/problems/merge-k-sorted-lists/
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
const mergeKSortedLL = (lists) => {
if(lists.length === 0) {
return null;
}
while(lists.length > 1) {
let list1 = lists.shift();
let list2 = lists.shift();
let merged = mergeLists(list1, list2);
lists.push(merged);
}
return lists[0]
}
const mergeLists = (list1, list2) => {
let dummy = new ListNode(0);
let head = dummy;
while(list1 && list2) {
if(list1.val <= list2.val) {
dummy.next = list1;
list1 = list1.next;
} else {
dummy.next = list2;
list2 = list2.next;
}
dummy = dummy.next;
}
if(list1 === null) {
dummy.next = list2;
} else {
dummy.next = list1;
}
return head.next;
}