Compare commits

...

5 Commits

Author SHA1 Message Date
AbdelRahman
89bf5e0678
Merge 87c1dde2ab into 2c67b48c21 2024-04-25 08:34:46 +08:00
AbdelRahman Hamed
87c1dde2ab separate test for offset-based insert 2023-04-08 05:44:39 +02:00
AbdelRahman Hamed
a03f49225d maintain current test coverage 2023-04-08 05:34:21 +02:00
AbdelRahman Hamed
dfdb6ec77d test adjusting tail after insert at tail position 2023-04-08 04:31:15 +02:00
AbdelRahman Hamed
019c07e9e3 set tail as newNode if currentNode is the tail 2023-04-08 02:21:33 +02:00
2 changed files with 15 additions and 0 deletions

View File

@ -75,6 +75,9 @@ export default class LinkedList {
if (currentNode) {
newNode.next = currentNode.next;
currentNode.next = newNode;
if (this.tail === currentNode) {
this.tail = newNode;
}
} else {
if (this.tail) {
this.tail.next = newNode;

View File

@ -47,6 +47,18 @@ describe('LinkedList', () => {
expect(linkedList.toString()).toBe('1,4,2,3,10');
});
it('should insert and maintain head and tail', () => {
const linkedList = new LinkedList();
linkedList.insert(2, 0);
linkedList.insert(3, 1);
linkedList.insert(4, 2);
expect(linkedList.toString()).toBe('2,3,4');
expect(linkedList.head.toString()).toBe('2');
expect(linkedList.tail.toString()).toBe('4');
});
it('should delete node by value from linked list', () => {
const linkedList = new LinkedList();