trekhleb / trekhleb/javascript-algorithms
bug(linked-list): `insert` method duplicates value of head in tail
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 197k
- Forks
- 31k
- PR merge metrics
- No merged PRs in 30d
Description
Hi, guys! Thank you for this awesome repository! The other day I found this problem
const list = new List();
list.insert(2, 0);
list.insert(3, 1);
list.insert(4, 2);
returns
{
"head": {
"data": 2,
"next": {
"data": 3,
"next": {
"data": 4,
"next": null
}
}
},
"tail": {
"data": 2,
"next": {
"data": 3,
"next": {
"data": 4,
"next": null
}
}
},
}
It solved if you add this code after line 77.
this.tail.next = newNode;
this.tail = newNode;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/data-structures/linked-list/LinkedList.js around line 77 and reproduce the reported insert sequence. Inspect how head and tail are updated when inserting at the end; done means the tail references the final node rather than duplicating the head while preserving the shown list order.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100