Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removeFirst() function in linked-list.js #13

Closed
keiseiTi opened this issue May 27, 2019 · 1 comment · Fixed by #15
Closed

removeFirst() function in linked-list.js #13

keiseiTi opened this issue May 27, 2019 · 1 comment · Fixed by #15

Comments

@keiseiTi
Copy link

When there is only one Node , this linkedlist is wrong.

  removeFirst() {
    const head = this.first;
    if (head) {
      this.first = head.next;
      if (this.first) {
        this.first.previous = null;
      }
      this.size -= 1;
    } else {
      this.last = null;
    }
    return head && head.value;
  }

The following code is correct.

  removeFirst() {
    const head = this.first;
    if (head) {
      this.first = head.next;
      if (this.first) {
        this.first.previous = null;
      }
      else {
        this.last = null;
      }
      this.size -= 1;
    }
    return head && head.value;
  }
@amejiarosario
Copy link
Owner

amejiarosario commented May 27, 2019

I added some unit test to verify the issue and added the fix. If you find anything else let me know or feel free to open a pull request. Thanks, @yupeilin123!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants