Skip to content

Commit 908c0d9

Browse files
committed
--update: remove from beginning
1 parent bf94b22 commit 908c0d9

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/_DataStructures_/LinkedList/LinkedList.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('Data Structures: Linked Lists', () => {
4747
list.addAtEnd(15);
4848
expect(list.head.data).toEqual(12);
4949
expect(list.head.next.data).toEqual(15);
50+
expect(list.length()).toEqual(2);
5051
});
5152

5253
it('Should return the present size of the list using list.length()', () => {
@@ -56,5 +57,15 @@ describe('Data Structures: Linked Lists', () => {
5657
list.addAtBeginning(3);
5758
expect(list.length()).toEqual(3);
5859
});
60+
61+
it('Should remove element at front using list.removeFromBeginning()', () => {
62+
list.addAtBeginning(12);
63+
expect(list.removeFromBeginning()).toEqual(12);
64+
65+
list.addAtBeginning(15);
66+
list.addAtBeginning(16);
67+
expect(list.removeFromBeginning()).toEqual(16);
68+
expect(list.length()).toEqual(1);
69+
});
5970
});
6071
});

src/_DataStructures_/LinkedList/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ class LinkedList {
4646
}
4747
return count;
4848
}
49+
50+
removeFromBeginning() {
51+
if (!this.head) {
52+
return null;
53+
}
54+
const temp = this.head;
55+
this.head = this.head.next;
56+
return temp.data;
57+
}
4958
}
5059

5160
module.exports = { LinkedList, Node };

0 commit comments

Comments
 (0)