Fix LinkedList.insert not updating the tail when inserting after the last node - #2220
Open
Darkslayer3324j wants to merge 1 commit into
Open
Darkslayer3324j wants to merge 1 commit into
Darkslayer3324j wants to merge 1 commit into
Conversation
…last node insert(value, index) with index equal to the list length linked the new node after the current tail but left this.tail pointing at the old last node, so a following append() overwrote the inserted node and it was lost. Update the tail in that case and add a test. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I found that
LinkedList.insert(value, index)corrupts the list when the valueis inserted right after the last node (
index === length): the new node islinked after the old tail, but
this.tailis not updated. The nextappend()then writes to the old tail's
next, overwriting the inserted node:insertalready handles an index beyond the end by appending and updatingtail(theelsebranch), andindex === 0viaprepend; only thisin-between case (walking to the last node and linking after it) missed the tail
update. I fixed it by setting
this.tail = newNodewhen the node was linkedafter the current tail.
I added a test for insert-then-append/deleteTail; I checked that it fails on
master(1 failed) and passes with the change (21 passed).eslintruns cleanvia the pre-commit hook.
I found this by differential fuzzing (random operation sequences on the data
structures compared against a plain-array model); the rest of that pass (heaps,
priority queue, hash table, Fenwick tree, disjoint set, LRU caches) matched the
model.
Assisted with Claude.