-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
92 lines (78 loc) · 1.78 KB
/
my_solution.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class ListNode {
constructor(val, next) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
}
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
const mergeKLists = (lists) => {
if (0 === lists.length) return null;
// if (1 === lists.length) return lists;
while (lists.length > 1) {
lists.push(mergetTwoLists(lists.shift(), lists.shift()));
}
// while (lists.length > 1) {
// console.log("___")
// console.log(lists)
// let mergedLists = mergetTwoLists(lists.shift(), lists.shift())
// lists.push(mergedLists);
// }
console.log("result")
console.log(lists[0])
return;
};
const mergetTwoLists = (ll1, ll2) => {
let dummy = new ListNode(), head = dummy;
while (ll1 && ll2) {
console.log(`ll1.val:${ll1.val}, ll2.val:${ll2.val}`)
if (ll1.val <= ll2.val) {
dummy.next = ll1;
ll1 = ll1.next
} else {
dummy.next = ll2;
ll2 = ll2.next
}
dummy = dummy.next;
// edge case
// console.log("result")
// console.log(result)
// console.log("ll1")
// console.log(ll1)
// console.log("ll2")
// console.log(ll2)
}
// console.log("ll1")
// console.log(ll1)
// console.log(ll1 === null)
// console.log("ll2")
// console.log(ll2)
// console.log(ll2 === null)
//
if (null === ll2) {
console.log("Hereeeee")
dummy.next = ll1;
} else {
dummy.next = ll2;
}
return head.next;
}
// [[1,4,5],[1,3,4],[2,6]]
// mergeKLists([
// new ListNode(1,
// new ListNode(4,
// new ListNode(5))),
// new ListNode(1,
// new ListNode(3,
// new ListNode(4))),
// new ListNode(2,
// new ListNode(6))
// ])
// mergeKLists([
// new ListNode(1,
// new ListNode(4,
// new ListNode(5)))
// ])
mergeKLists([])