trekhleb / javascript-algorithms

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
MIT License
185.07k stars 29.85k forks source link

set tail as newNode if currentNode is the tail #1020

Open xyn22 opened 1 year ago

xyn22 commented 1 year ago

closes #1016 closes #1004

To reproduce

  1. Populate the list normally, insert few values.
  2. call insert with rawIndex equals to last node index + 1 (exactly where new node would fit)

Expectation

this.tail should point to the last added value as it has rawIndex as previous tail + 1

Behaviour

this.tail points to the previous tail.

Fix

If currentNode is the tail then newNode should be the new tail, as in this scenario currentNode will have newNode as its next