-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked-list-recursive.js
77 lines (67 loc) · 1.97 KB
/
linked-list-recursive.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
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
append(val) {
if (this.head === null) {
this.head = new Node(val);
return;
}
this._append(val, this.head);
}
// вспомогательная функция для рекурсивных вызовов метода Append
_append(val, curr) {
if (curr.next === null) {
curr.next = new Node(val);
return;
}
this._append(val, curr.next);
}
print() {
const output = this._print(this.head);
console.log(output);
}
// вспомогательная функция для рекурсивных вызовов метода Print
_print(curr) {
if (curr === null) return "";
return curr.val + "-> " + this._print(curr.next);
}
contains(target) {
return this._contains(target, this.head);
}
// вспомогательная функция для рекурсивных вызовов метода поиска элемента - Contains
_contains(target, curr) {
if (curr === null) return false;
if (curr.val === target) return true;
return this._contains(target, curr.next);
}
}
const list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
console.log(list.head);
list.print();
console.log(list.contains(9));
console.log(list.contains(2));
const sumListRecursive = (head) => {
if (head === null) return 0;
return head.val + sumListRecursive(head.next);
};
const sumListInterative = (head) => {
let sum = 0;
let curr = head;
while (curr !== null) {
sum += curr.val;
curr = curr.next;
}
return sum;
};
console.log(sumListInterative(list.head));
console.log(sumListRecursive(list.head));