-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.js
176 lines (150 loc) · 3.55 KB
/
linked-list.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
insert(value) {
let current = this.head;
let newNode = new Node(value);
if (!current) {
this.head = newNode;
} else {
newNode.next = current;
this.head = newNode;
}
}
includes(value) {
let current = this.head;
while (current) {
if (value === current.value) { return true; }
else if (current.next === null) { return false; }
else { current = current.next; }
}
}
toString() {
let current = this.head;
let string = '';
while (current) {
const banana = current.value;
current = current.next;
string += `{ ${banana} } -> `;
}
string += '{NULL}';
// console.log(string);
return string;
}
append(value) {
let current = this.head;
while (current) {
if (current.next === null) {
current.next = new Node(value);
return;
}
current = current.next;
}
}
insertBefore(value, newVal) {
let current = this.head;
let newNode = new Node(newVal);
while (current.next !== null) {
if (current.value === value) {
newNode.next = current;
this.head = newNode;
} else if (current.next.value === value) {
let temp = current.next;
current.next = newNode;
newNode.next = temp;
}
current = current.next;
}
}
insertAfter(value, newVal) {
let current = this.head;
let newNode = new Node(newVal);
while (current) {
if (current.value === value) {
let temp = current.next;
current.next = newNode;
newNode.next = temp;
}
current = current.next;
}
}
kthFromEnd(k) {
if (this.head === null || k < 1) {
return null;
}
let pointerOne = this.head;
let pointerTwo = this.head;
for (let i = 0; i < k - 1; i++) {
if (pointerTwo === null) {
return null;
}
pointerTwo = pointerTwo.next;
}
while (pointerTwo.next !== null) {
pointerOne = pointerOne.next;
pointerTwo = pointerTwo.next;
}
// console.log('ONE', pointerOne);
return pointerOne.value;
// console.log('COUNT', getCount());
}
printUnique() {
let current = this.head;
while (current) {
let counter = 0;
let temp = current;
while (current) {
if (current.value !== temp.value) {
counter++;
}
current = current.next;
}
return counter;
}
}
}
const ll = new LinkedList();
ll.head = new Node(0);
// head: (0) => null
ll.head.next = new Node(1);
// head: (0) => (1) => null
ll.head.next.next = new Node(2);
// head: (0) => (1) => (2) => null
ll.head.next.next.next = new Node(1);
// head: (0) => (1) => (2) => (3) => null
ll.head.next.next.next.next = new Node(0);
// head: (0) => (1) => (2) => (3) => (4) => null
// function reversedArray(arr) {
// const result = [];
// for (let i = arr.length - 1; i >= 0; i--) {
// result[result.length] = arr[i];
// }
// return result;
// }
// function isPalindrome(LL) {
// let current = LL.head;
// let arr = [];
// while (current) {
// arr[arr.length] = current.value;
// current = current.next;
// }
// let reverse = reversedArray(arr);
// for (let i = 0; i < arr.length; i++) {
// return !!(arr[i] === reverse[i]);
// }
// }
// console.log(isPalindrome(ll));
// ll.kthFromEnd(2);
// console.log(ll.printUnique());
module.exports = {
ll: LinkedList,
node: Node,
};