Skip to content

Commit dce3870

Browse files
committed
update: added filter method for LL
1 parent b3ef051 commit dce3870

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/_DataStructures_/Graphs/index.js

Whitespace-only changes.

src/_DataStructures_/LinkedList/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,31 @@ class LinkedList {
151151
return node;
152152
}
153153

154+
filter(value) {
155+
if (!this.head) {
156+
return null;
157+
}
158+
159+
if (this.head.data === value) {
160+
this.head = this.head.next;
161+
this.size -= 1;
162+
}
163+
164+
let { head } = this;
165+
let previous = null;
166+
167+
while (head !== null) {
168+
if (head.data === value) {
169+
previous.next = head.next;
170+
this.size -= 1;
171+
}
172+
previous = head;
173+
head = head.next;
174+
}
175+
176+
return this.head;
177+
}
178+
154179
length() {
155180
return this.size;
156181
}

0 commit comments

Comments
 (0)