amejiarosario/dsa.js-data-structures-algorithms-javascript

removeFirst() function in linked-list.js

keiseiTi opened this issue · 1 comments

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;
  }

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!