Skip to content

fix: update tail when inserting at the end of LinkedList#2106

Open
yorunohoshi wants to merge 1 commit intotrekhleb:masterfrom
yorunohoshi:fix/linked-list-insert-tail
Open

fix: update tail when inserting at the end of LinkedList#2106
yorunohoshi wants to merge 1 commit intotrekhleb:masterfrom
yorunohoshi:fix/linked-list-insert-tail

Conversation

@yorunohoshi
Copy link

Description

In the LinkedList.insert() method, the tail pointer was not being updated when a new node was inserted at the very end of the list (at index = length). This caused an inconsistency where tail remained pointed to the second-to-last node.

Impact

If append() is called immediately after such an insert(), the newly appended node would overwrite the node previously inserted by insert(), leading to data loss.

Changes

  • Updated insert() to check if the new node is inserted at the tail position and update this.tail accordingly.
  • Added a logic to handle tail updates even when inserting into an empty list or out of bounds.
  • Added a new test case in LinkedList.test.js to verify tail consistency and prevent regression.

Checklist

  • My code follows the code style of this project (npm run lint passed).
  • I have performed a self-review of my own code.
  • I have added a test that proves my fix is effective.
  • All new and existing tests passed (npm test passed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant