Skip to content

Commit 6ce33d6

Browse files
committed
adding removeFirst and removeLast method
1 parent 4e59e81 commit 6ce33d6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

linkedList/linkedList.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ class LinkedList {
4949
clear() {
5050
this.head = null;
5151
}
52+
53+
removeFirst() {
54+
if(!this.head) {
55+
return;
56+
}
57+
58+
this.head = this.head.next;
59+
}
60+
61+
removeLast() {
62+
63+
if(!this.head) {
64+
return;
65+
} else if(!this.head.next) {
66+
previous = null;
67+
return;
68+
}
69+
70+
let previous = this.head;
71+
let node = this.head.next;
72+
73+
while(node.next) {
74+
previous = node;
75+
node = node.next;
76+
}
77+
78+
previous.next = null;
79+
}
5280
}
5381
}
5482

0 commit comments

Comments
 (0)