trekhleb / trekhleb/javascript-algorithms

bug(linked-list): `insert` method duplicates value of head in tail

Open
#1,016 3 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.