From 019c07e9e3bdd804b6d2ba2adab42f69cfc25bbd Mon Sep 17 00:00:00 2001 From: AbdelRahman Hamed Date: Sat, 8 Apr 2023 02:21:33 +0200 Subject: [PATCH 1/4] set tail as newNode if currentNode is the tail --- src/data-structures/linked-list/LinkedList.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index ba7d0e3ee1..39de5b4671 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -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; From dfdb6ec77d6ab2da81b93ccfe8b294d7576f95f7 Mon Sep 17 00:00:00 2001 From: AbdelRahman Hamed Date: Sat, 8 Apr 2023 04:31:15 +0200 Subject: [PATCH 2/4] test adjusting tail after insert at tail position --- src/data-structures/linked-list/__test__/LinkedList.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index 6ac41ddf05..8971498262 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -43,8 +43,10 @@ describe('LinkedList', () => { linkedList.insert(2, 1); linkedList.insert(1, -7); linkedList.insert(10, 9); - - expect(linkedList.toString()).toBe('1,4,2,3,10'); + linkedList.insert(7, 5); + expect(linkedList.toString()).toBe('1,4,2,3,10,7'); + expect(linkedList.head.toString()).toBe('1'); + expect(linkedList.tail.toString()).toBe('7'); }); it('should delete node by value from linked list', () => { From a03f49225dcbccb05e667c986141d57cb812d544 Mon Sep 17 00:00:00 2001 From: AbdelRahman Hamed Date: Sat, 8 Apr 2023 05:34:21 +0200 Subject: [PATCH 3/4] maintain current test coverage --- src/data-structures/linked-list/__test__/LinkedList.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index 8971498262..c2ca574ef0 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -43,6 +43,9 @@ describe('LinkedList', () => { linkedList.insert(2, 1); linkedList.insert(1, -7); linkedList.insert(10, 9); + + expect(linkedList.toString()).toBe('1,4,2,3,10'); + linkedList.insert(7, 5); expect(linkedList.toString()).toBe('1,4,2,3,10,7'); expect(linkedList.head.toString()).toBe('1'); From 87c1dde2ab5b8512b88f92949ffeb0ea3d9c2ab6 Mon Sep 17 00:00:00 2001 From: AbdelRahman Hamed Date: Sat, 8 Apr 2023 05:44:39 +0200 Subject: [PATCH 4/4] separate test for offset-based insert --- .../linked-list/__test__/LinkedList.test.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index c2ca574ef0..bf411e24a5 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -45,11 +45,18 @@ describe('LinkedList', () => { linkedList.insert(10, 9); expect(linkedList.toString()).toBe('1,4,2,3,10'); + }); - linkedList.insert(7, 5); - expect(linkedList.toString()).toBe('1,4,2,3,10,7'); - expect(linkedList.head.toString()).toBe('1'); - expect(linkedList.tail.toString()).toBe('7'); + 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', () => {