-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly-linked-list.js
168 lines (136 loc) · 2.99 KB
/
singly-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
'use strict';
class Node {
constructor (value, next = null) {
this.value = value;
this.next = next;
}
}
export default class List {
constructor () {
this.first = null;
}
get values () {
let values = '';
if (!this.isEmpty()) {
let current = this.first;
while (current != null) {
values += JSON.stringify(current.value) + ',';
current = current.next;
}
values = values.slice(0, -1);
}
return values;
}
/**
* Adds a value to the end of the linked list
* @param {*}
*/
add (value) {
let n = typeof value === 'Node' ? value : new Node(value);
if (this.isEmpty()) {
this.first = n;
} else {
let current = this.first;
while (current.next != null) {
current = current.next;
}
current.next = n;
}
}
/**
* Adds a value at first position of list
* @param {*} value
*/
addFirst (value) {
let n = new Node(value, this.first);
this.first = n;
}
/**
* Adds a value into a specified position in the list
* @param {*} value
* @param {Number} position
*/
addAtPosition (value, position) {
let n = new Node(value);
let index = 1;
let current = this.first;
if (position === 0) {
this.addFirst(value);
return;
}
while (current != null) {
if (index < position) {
current = current.next;
index++;
} else if (index === position) {
let tmp = current.next;
current.next = n;
n.next = tmp;
break;
}
}
if ((index !== position && position !== 0) || current === null) {
throw new Error('Unable to add at position ' + position + ', list is of length ' + (index === 1 ? 0 : index));
}
}
/**
* Search for a value in the list
* @param {*} value
* @return {Node}
*/
search (value) {
let current = this.first;
while (current !== null) {
if (current.value === value) {
return current;
}
current = current.next;
}
return false;
}
/**
* Delete a value from the list
* @param {*} value
* @return {Boolean}
*/
delete (value) {
let hasDeletedNode = false;
if (!this.isEmpty() && this.first.value === value) {
this.first = this.first.next;
hasDeletedNode = true;
}
let previous = null;
let current = this.first;
while (current !== null) {
if (current.value === value) {
previous.next = current.next;
hasDeletedNode = true;
} else {
previous = current;
}
current = current.next;
}
return hasDeletedNode;
}
/**
* Reverse the linked list
*/
reverse () {
let previous = null;
let current = this.first;
let next = null;
while (current !== null) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
this.first = previous;
}
/**
* @return {Boolean}
*/
isEmpty () {
return this.first === null;
}
};