Skip to content

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

Closed
@keiseiTi

Description

@keiseiTi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions