-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathlinked-list.js
169 lines (156 loc) · 2.99 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
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
isEmpty() {
return this.size === 0;
}
getSize() {
return this.size;
}
prepend(value) {
const node = new Node(value);
if (this.isEmpty()) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
this.size++;
}
append(value) {
const node = new Node(value);
if (this.isEmpty()) {
this.head = node;
} else {
let curr = this.head;
while (curr.next) {
curr = curr.next;
}
curr.next = node;
}
this.size++;
}
insert(value, index) {
if (index < 0 || index > this.size) {
return;
}
if (index === 0) {
this.prepend(value);
} else {
const node = new Node(value);
let prev = this.head;
for (let i = 0; i < index - 1; i++) {
prev = prev.next;
}
node.next = prev.next;
prev.next = node;
this.size++;
}
}
removeFrom(index) {
if (index < 0 || index >= this.size) {
return null;
}
let removedNode;
if (index === 0) {
removedNode = this.head;
this.head = this.head.next;
} else {
let prev = this.head;
for (let i = 0; i < index - 1; i++) {
prev = prev.next;
}
removedNode = prev.next;
prev.next = removedNode.next;
}
this.size--;
return removedNode.value;
}
removeValue(value) {
if (this.isEmpty()) {
return null;
}
if (this.head.value === value) {
this.head = this.head.next;
this.size--;
return value;
} else {
let prev = this.head;
while (prev.next && prev.next.value !== value) {
prev = prev.next;
}
if (prev.next) {
removedNode = prev.next;
prev.next = removedNode.next;
this.size--;
return value;
}
return null;
}
}
search(value) {
if (this.isEmpty()) {
return -1;
}
let i = 0;
let curr = this.head;
while (curr) {
if (curr.value === value) {
return i;
}
curr = curr.next;
i++;
}
return -1;
}
reverse() {
let prev = null;
let curr = this.head;
while (curr) {
let next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
this.head = prev;
}
print() {
if (this.isEmpty()) {
console.log("List is empty");
} else {
let curr = this.head;
let list = "";
while (curr) {
list += `${curr.value}->`;
curr = curr.next;
}
console.log(list);
}
}
}
const l = new LinkedList();
console.log(l.isEmpty());
l.append(50);
l.prepend(20);
l.append(80);
l.insert(60, 2);
console.log(l.getSize());
l.print();
l.reverse();
l.print();
console.log(l.search(60));
l.removeFrom(4);
console.log(l.getSize());
l.print();
l.removeValue(80);
l.print();
console.log(l.getSize());
l.print();