forked from ludralph/singly-linked-list-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglyLinkedList.js
198 lines (181 loc) · 3.66 KB
/
singlyLinkedList.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
// this.head = {
// value: initValue,
// next: null,
// };
this.head = null;
this.tail = this.head;
this.length = 0;
}
/**
* add a node at the end
* @param {*} value
* @returns
*/
push(value) {
if (this.length === 0) {
this.head = new Node(value);
this.tail = this.head;
this.length++;
return this;
}
const newTail = new Node(value);
this.tail.next = newTail;
this.tail = newTail;
this.length++;
return this;
}
/**
* add a node to the beginning
* @param {*} value
* @returns
*/
unshift(value) {
const newHead = new Node(value);
newHead.next = this.head;
this.head = newHead;
this.length++;
return this;
}
/**
* should remove a node at the beginning
* @returns
*/
shift() {
// check
if (this.length < 1) {
console.warn('Nothing to remove');
return undefined;
}
const currHead = this.head;
if (this.length === 1) {
this.head = null;
this.tail = this.head;
this.length--;
return currHead.value;
}
const newHead = this.head.next;
newHead.next = this.head.next.next;
this.head = newHead;
this.length--;
return currHead.value;
}
/**
* should remove a node at the end
* @returns
*/
pop() {
// check
if (this.length < 1) {
console.warn('Nothing to remove');
return undefined;
}
const removed = this.tail;
this.remove(this.length - 1);
return removed.value;
}
/**
* Remove a node by given index
* @param {Number} index
* @returns
*/
remove(index) {
// check params
const leader = this._get(index - 1);
const unwantedNode = leader.next;
leader.next = unwantedNode.next;
this.length--;
return this;
}
/**
* Update the value of a node at a given index
* @param {Number} index
* @param {*} value
*/
set(index, value) {
// check
if (index > this.length) {
return false;
}
const node = this._get(index);
node.value = value;
return true;
}
/**
* Reverse all of the nodes
* @returns
*/
reverse() {
if (!this.head.next) {
return this.head;
}
let first = this.head;
this.tail = this.head;
let second = first.next;
while (second) {
const temp = second.next;
second.next = first;
first = second;
second = temp;
}
this.head.next = null;
this.head = first;
return this;
}
/**
* Find a node at given index
* @param {Number} index
* @returns
*/
_get(index) {
// check params
let counter = 0;
let currentNode = this.head;
while (counter !== index) {
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
/**
* Insert a node at given index with given value
* @param {Number} index
* @param {*} value
*/
_insert(index, value) {
// check params
if (index >= this.length) {
return this.push(value);
}
if (index === 0) {
return this.unshift(value);
}
const newNode = new Node(value);
const leader = this._get(index - 1);
const holdingPoiner = leader.next;
leader.next = newNode;
newNode.next = holdingPoiner;
this.length++;
return this;
}
/**
* Convert link list to array
* @returns {Array}
*/
convertToArray() {
const array = [];
let currentNode = this.head;
while (currentNode !== null) {
array.push(currentNode.value);
currentNode = currentNode.next;
}
return array;
}
}