|
1 |
| -# zipper lists |
| 1 | +# merge lists |
2 | 2 |
|
3 |
| -Write a function, mergelists, that takes in the head of two linked lists as arguments. The function should zipper the two lists together into single linked list by alternating nodes. If one of the linked lists is longer than the other, the resulting list should terminate with the remaining nodes. The function should return the head of the zippered linked list. |
| 3 | +Write a function, mergeLists, that takes in the head of two sorted linked lists as arguments. The function should merge the two lists together into single sorted linked list. The function should return the head of the merged linked list. |
4 | 4 |
|
5 | 5 | Do this in-place, by mutating the original Nodes.
|
6 | 6 |
|
7 |
| -You may assume that both input lists are non-empty. |
| 7 | +You may assume that both input lists are non-empty and contain increasing sorted numbers. |
8 | 8 |
|
9 | 9 | test_00:
|
10 | 10 | ```js
|
11 |
| -const a = new Node("a"); |
12 |
| -const b = new Node("b"); |
13 |
| -const c = new Node("c"); |
| 11 | +const a = new Node(5); |
| 12 | +const b = new Node(7); |
| 13 | +const c = new Node(10); |
| 14 | +const d = new Node(12); |
| 15 | +const e = new Node(20); |
| 16 | +const f = new Node(28); |
14 | 17 | a.next = b;
|
15 | 18 | b.next = c;
|
16 |
| -// a -> b -> c |
17 |
| - |
18 |
| -const x = new Node("x"); |
19 |
| -const y = new Node("y"); |
20 |
| -const z = new Node("z"); |
21 |
| -x.next = y; |
22 |
| -y.next = z; |
23 |
| -// x -> y -> z |
| 19 | +c.next = d; |
| 20 | +d.next = e; |
| 21 | +e.next = f; |
| 22 | +// 5 -> 7 -> 10 -> 12 -> 20 -> 28 |
| 23 | + |
| 24 | +const q = new Node(6); |
| 25 | +const r = new Node(8); |
| 26 | +const s = new Node(9); |
| 27 | +const t = new Node(25); |
| 28 | +q.next = r; |
| 29 | +r.next = s; |
| 30 | +s.next = t; |
| 31 | +// 6 -> 8 -> 9 -> 25 |
24 | 32 |
|
25 |
| -mergelists(a, x); |
26 |
| -// a -> x -> b -> y -> c -> z |
| 33 | +mergeLists(a, q); |
| 34 | +// 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 12 -> 20 -> 25 -> 28 |
27 | 35 | ```
|
28 | 36 |
|
29 | 37 | test_01:
|
30 | 38 | ```js
|
31 |
| -const a = new Node("a"); |
32 |
| -const b = new Node("b"); |
33 |
| -const c = new Node("c"); |
34 |
| -const d = new Node("d"); |
35 |
| -const e = new Node("e"); |
36 |
| -const f = new Node("f"); |
| 39 | +const a = new Node(5); |
| 40 | +const b = new Node(7); |
| 41 | +const c = new Node(10); |
| 42 | +const d = new Node(12); |
| 43 | +const e = new Node(20); |
| 44 | +const f = new Node(28); |
37 | 45 | a.next = b;
|
38 | 46 | b.next = c;
|
39 | 47 | c.next = d;
|
40 | 48 | d.next = e;
|
41 | 49 | e.next = f;
|
42 |
| -// a -> b -> c -> d -> e -> f |
43 |
| - |
44 |
| -const x = new Node("x"); |
45 |
| -const y = new Node("y"); |
46 |
| -const z = new Node("z"); |
47 |
| -x.next = y; |
48 |
| -y.next = z; |
49 |
| -// x -> y -> z |
50 |
| - |
51 |
| -mergelists(a, x); |
52 |
| -// a -> x -> b -> y -> c -> z -> d -> e -> f |
53 |
| -``` |
54 |
| - |
55 |
| -test_02: |
56 |
| -```js |
57 |
| -const s = new Node("s"); |
58 |
| -const t = new Node("t"); |
| 50 | +// 5 -> 7 -> 10 -> 12 -> 20 -> 28 |
| 51 | + |
| 52 | +const q = new Node(1); |
| 53 | +const r = new Node(8); |
| 54 | +const s = new Node(9); |
| 55 | +const t = new Node(10); |
| 56 | +q.next = r; |
| 57 | +r.next = s; |
59 | 58 | s.next = t;
|
60 |
| -// s -> t |
61 |
| - |
62 |
| -const one = new Node(1); |
63 |
| -const two = new Node(2); |
64 |
| -const three = new Node(3); |
65 |
| -const four = new Node(4); |
66 |
| -one.next = two; |
67 |
| -two.next = three; |
68 |
| -three.next = four; |
69 |
| -// 1 -> 2 -> 3 -> 4 |
| 59 | +// 1 -> 8 -> 9 -> 10 |
70 | 60 |
|
71 |
| -mergelists(s, one); |
72 |
| -// s -> 1 -> t -> 2 -> 3 -> 4 |
| 61 | +mergeLists(a, q); |
| 62 | +// 1 -> 5 -> 7 -> 8 -> 9 -> 10 -> 10 -> 12 -> 20 -> 28 |
73 | 63 | ```
|
74 | 64 |
|
75 |
| -test_03: |
76 |
| -```js |
77 |
| -const w = new Node("w"); |
78 |
| - |
79 |
| -// w |
80 |
| - |
81 |
| -const one = new Node(1); |
82 |
| -const two = new Node(2); |
83 |
| -const three = new Node(3); |
84 |
| -one.next = two; |
85 |
| -two.next = three; |
86 |
| -// 1 -> 2 -> 3 |
87 |
| - |
88 |
| -mergelists(w, one); |
89 |
| -// w -> 1 -> 2 -> 3 |
90 |
| -``` |
91 |
| - |
92 |
| -test_04: |
| 65 | +test_02: |
93 | 66 | ```js
|
94 |
| -const one = new Node(1); |
95 |
| -const two = new Node(2); |
96 |
| -const three = new Node(3); |
97 |
| -one.next = two; |
98 |
| -two.next = three; |
99 |
| -// 1 -> 2 -> 3 |
| 67 | +const h = new Node(30); |
| 68 | +// 30 |
100 | 69 |
|
101 |
| -const w = new Node("w"); |
102 |
| -// w |
| 70 | +const p = new Node(15); |
| 71 | +const q = new Node(67); |
| 72 | +p.next = q; |
| 73 | +// 15 -> 67 |
103 | 74 |
|
104 |
| -mergelists(one, w); |
105 |
| -// 1 -> w -> 2 -> 3 |
| 75 | +mergeLists(h, p); |
| 76 | +// 15 -> 30 -> 67 |
106 | 77 | ```
|
0 commit comments