-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list-problem.js
More file actions
88 lines (73 loc) · 1.62 KB
/
linked_list-problem.js
File metadata and controls
88 lines (73 loc) · 1.62 KB
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
// print node datam return it inside an array:
const linkedListValues = (head) => {
const arr = [];
let current = head;
while (current !== null) {
arr.push(current.data);
current = current.next;
}
console.log(arr);
return arr;
};
// summation of the linked list values:
const sumList = (head) => {
let sum = 0;
let current = head;
while (current !== null) {
sum += current.data;
current = current.next;
}
return sum;
};
// linked list find:
const linkedListFind = (head, target) => {
let current = head;
while (current !== null) {
if (current.data === target) return true;
}
return false;
};
// get node values:
const getNodeValues = (head, index) => {
let current = head;
let count = 0;
while (current !== null) {
if (count === index) return current.data;
count += 1;
current = current.next;
}
return null;
};
// reverse linked list:
const reverseList = (head) => {
let prev = null;
let current = head;
while (current !== null) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
};
// zipper list:
const zipperList = (head1, head2) => {
let tail = head1;
let current1 = head1.next;
let current2 = head2;
let count = 0;
while (current1 !== null && current2 !== null) {
if (count % 2 === 0) {
tail.next = current2;
current2 = current2.next;
} else {
tail.next = current1;
current1 = current1.next;
}
tail = tail.next;
count += 1;
}
if (current1 !== null) tail.next = current1;
if (current2 !== null) tail.next = current2;
return head1;
};