Skip to content

Commit

Permalink
fix(LinkedList): update this.last after remove last element
Browse files Browse the repository at this point in the history
  • Loading branch information
samavati committed Apr 20, 2022
1 parent 40ca4e2 commit 9614ceb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/LinkedList/LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ export class LinkedList<T = any> {
if (currentNode.next.isEqual(value)) {
currentNode.next = currentNode.next.next;
this._length--;
return;
break;
} else {
currentNode = currentNode.next;
}
}

// check if we should update this._tail
if (this._tail?.isEqual(value)) {
this._tail = currentNode;
}

} else {
if (!this._head) {
return false;
Expand All @@ -162,6 +167,10 @@ export class LinkedList<T = any> {
while (currentNode.next) {
// Deleting the node by delete it's reference in previous node
if (currentNode.next.value === value) {
// check if we should update this._tail
if (this._tail?.isEqual(currentNode.next)) {
this._tail = currentNode.next.next;
}
currentNode.next = currentNode.next.next;
this._length--;
return true
Expand All @@ -170,7 +179,7 @@ export class LinkedList<T = any> {
}
}

return false
return false;
}
}

Expand Down

0 comments on commit 9614ceb

Please sign in to comment.