-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-024-zipperLists.js
111 lines (87 loc) · 2.17 KB
/
structy-024-zipperLists.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// https://structy.net/problems/zipper-lists
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
// p: 2 heads of ll
// r: 1 head of ll
// a b ---------------> c
// h1 n1
// c1
// x y ---------------> z
// h2 n2
// c2
// // recursion:
const zipperLists = (head1, head2) => {
if (!head1 && !head2) return null;
if (!head1) return head2;
if (!head2) return head1;
let next1 = head1.next;
let next2 = head2.next;
head1.next = head2;
head2.next = zipperLists(next1, next2);
return head1;
};
// // recursion: WRONG
// const zipperLists = (head1, head2) => {
// if (!head1 && !head2) return null;
// if (!head1) return head2;
// if (!head2) return head1;
// let next1 = head1.next;
// let next2 = head2.next;
// head1.next = head2;
// head2.next = next1;
// // console.log(head1, head2);
// zipperLists(next1, next2);
// return head1;
// };
// // iteration
// const zipperLists = (head1, head2) => {
// let current1 = head1;
// let current2 = head2;
// let tail = null;
// while (current1 && current2) {
// let next1 = current1.next;
// let next2 = current2.next;
// current1.next = current2;
// current1 = current2;
// current2 = next1;
// }
// return head1;
// };
// const a = new Node("a");
// const b = new Node("b");
// const c = new Node("c");
// a.next = b;
// b.next = c;
// // a -> b -> c
// const x = new Node("x");
// const y = new Node("y");
// const z = new Node("z");
// x.next = y;
// y.next = z;
// // x -> y -> z
// console.log(zipperLists(a, x));
// // a -> x -> b -> y -> c -> z
const a = new Node("a");
const b = new Node("b");
const c = new Node("c");
const d = new Node("d");
const e = new Node("e");
const f = new Node("f");
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = f;
// a -> b -> c -> d -> e -> f
const x = new Node("x");
const y = new Node("y");
const z = new Node("z");
x.next = y;
y.next = z;
// x -> y -> z
console.log(zipperLists(a, x));
// a -> x -> b -> y -> c -> z -> d -> e -> f