This commit is contained in:
AbdelRahman 2024-07-14 12:28:11 +03:00 committed by GitHub
commit da6d01c8fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 0 deletions

View File

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

View File

@ -47,6 +47,18 @@ describe('LinkedList', () => {
expect(linkedList.toString()).toBe('1,4,2,3,10'); 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', () => { it('should delete node by value from linked list', () => {
const linkedList = new LinkedList(); const linkedList = new LinkedList();