Repository navigation
Expand file tree
/
Copy pathlinkedList.js
More file actions
205 lines (182 loc) · 3.91 KB
/
Copy pathlinkedList.js
File metadata and controls
205 lines (182 loc) · 3.91 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Class representing a tree node
*/
class Node {
/**
* Create a linked list node
* @param {any} value - The value of the node
* @param {Node} next - The reference to the next nodes
*/
constructor(value, next = null) {
this.value = value;
this.next = next;
}
}
/**
* Class representing a linked list
*/
class LinkedList {
constructor() {
this.head = null;
}
/**
* Get the size of the linked list.
* @returns {number} The number of nodes in the linked list.
*/
size() {
let count = 0;
let node = this.head;
while (node) {
count++;
node = node.next;
}
return count;
}
/**
* Get the first node in the linked list.
* @returns {Node} The first node in the linked list.
*/
getFirst() {
return this.head;
}
/**
* Get the last node in the linked list.
* @returns {Node} The last node in the linked list or null if the list is empty.
*/
getLast() {
if (!this.head) {
return null;
}
let node = this.head;
while (node.next) {
node = node.next;
}
return node;
}
/**
* Get the node at a specific index.
* @param {number} index The index of the node to get.
* @returns {Node} The node at the specified index or null if list is empty or index is out of boundary.
*/
getAt(index) {
let counter = 0;
let node = this.head;
while (node) {
if (counter === index) {
return node;
}
node = node.next;
counter++;
}
return null;
}
/**
* Search for a node with a specific value.
* @param {any} value
* @returns {boolean} True if the value is found, false otherwise.
*/
search(value) {
let node = this.head;
while (node) {
if (node.value === value) {
return true;
}
node = node.next;
}
return false;
}
/**
* Empty the linked list. i.e. Set the head as null.
*/
clear() {
this.head = null;
}
/**
* Insert a node at a specific index.
* @param {any} value
* @param {number} index
*/
insertAt(value, index) {
if (!this.head) {
// When the list is empty.
this.head = new Node(value);
return;
}
if (index === 0) {
// When inserting at the head.
this.head = new Node(value, this.head);
return;
}
const previous = this.getAt(index - 1) || this.getLast();
const node = new Node(value, previous.next);
previous.next = node;
}
/**
* Add a node to the head of the linked list
* @param {any} value - The value of the node to insert
*/
insertFirst(value) {
this.head = new Node(value, this.head);
}
/**
* Append a node to the end of the linked list
* @param {any} value The value of the node to insert
*/
insertLast(value) {
const last = this.getLast();
if (last) {
last.next = new Node(value);
} else {
this.head = new Node(value);
}
}
/**
* Remove the first node in the linked list.
*/
removeFirst() {
if (!this.head) {
return;
}
this.head = this.head.next;
}
/**
* Remove the last node in the linked list. (Set the last second node's next as null)
*/
removeLast() {
if (!this.head) {
//When linked list is empty
return;
}
let node = this.head;
if (!node.next) {
// When linked list has only one node
this.head = null;
return;
}
while (node.next.next) {
node = node.next;
}
node.next = null;
}
/**
* Remove a node at a specific index.
* @param {number} index
*/
removeAt(index) {
if (!this.head) {
//When linked list is empty
return;
}
if (index === 0) {
// Delete the first node.
this.head = this.head.next;
return;
}
const previous = this.getAt(index - 1);
if (!previous || !previous.next) {
return;
}
previous.next = previous.next.next;
}
}
module.exports = { Node, LinkedList };