-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-025-mergeLists-2.js
90 lines (76 loc) · 1.7 KB
/
structy-025-mergeLists-2.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
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
// 5 7 -> 10 -> 12 -> 20 -> 28
// h1| c1 n1
// 6 -> 8 -> 9 -> 25
// h2 c2 n2
// recursive O(min(n,m)) O(min(n,m))
const mergeLists = (head1, head2) => {
if (!head1 && !head2) return null;
if (!head1) return head2;
if (!head2) return head1;
let n1 = head1.next;
let n2 = head2.next;
if (head1.val < head2.val) {
head1.next = mergeLists(n1, head2);
return head1;
} else {
head2.next = mergeLists(head1, n2);
return head2;
}
};
// iterative O(min(n,m)) O(1)
// const mergeLists = (head1, head2) => {
// let c1 = head1;
// let c2 = head2;
// let head = new Node(0);
// let tail = head;
// while (c1 && c2) {
// let n1 = c1.next;
// let n2 = c2.next;
// if (c1.val < c2.val) {
// tail.next = c1;
// tail = c1;
// c1.next = c2;
// c1 = n1;
// } else {
// tail.next = c2;
// tail = c2;
// c2.next = c1;
// c2 = n2;
// }
// }
// // if (c1) tail.next = c1;
// // if (c2) tail.next = c2;
// return head.next;
// };
// // iterative O(min(n,m)) O(1)
// const mergeLists = (head1, head2) => {
// let c1 = head1;
// let c2 = head2;
// let tail = null;
// let head = new Node(0);
// tail = head;
// while (c1 && c2) {
// let n1 = c1.next;
// let n2 = c2.next;
// if (c1.val < c2.val) {
// tail.next = c1;
// tail = c1;
// c1.next = c2;
// c1 = n1;
// } else {
// tail.next = c2;
// tail = c2;
// c2.next = c1;
// c2 = n2;
// }
// }
// // if (c1) tail.next = c1;
// // if (c2) tail.next = c2;
// return head.next;
// };