Skip to content

Commit 29ad87a

Browse files
author
Julian Popov
committed
Problem: merge lists
1 parent b1a3861 commit 29ad87a

File tree

1 file changed

+50
-79
lines changed

1 file changed

+50
-79
lines changed

017-merge-lists/README.md

Lines changed: 50 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,77 @@
1-
# zipper lists
1+
# merge lists
22

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.
44

55
Do this in-place, by mutating the original Nodes.
66

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.
88

99
test_00:
1010
```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);
1417
a.next = b;
1518
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
2432

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
2735
```
2836

2937
test_01:
3038
```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);
3745
a.next = b;
3846
b.next = c;
3947
c.next = d;
4048
d.next = e;
4149
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;
5958
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
7060

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
7363
```
7464

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:
9366
```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
10069

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
10374

104-
mergelists(one, w);
105-
// 1 -> w -> 2 -> 3
75+
mergeLists(h, p);
76+
// 15 -> 30 -> 67
10677
```

0 commit comments

Comments
 (0)