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

removeByPosition () function in linked-list.js

keiseiTi opened this issue · 1 comments

https://github.com/amejiarosario/dsa.js/blob/d4a5037740b6efbd634e222fc645190e4244e40f/src/data-structures/linked-lists/linked-list.js#L225-L236

The following code is correct.

  removeByPosition(position = 0) {
    const current = this.get(position);
    if (position === 0) {
      this.removeFirst();
    } else if (position === this.size) {
      this.removeLast();
    }else if(current) {
      current.previous.next = current.next;
      current.next.previous = current.previous;
      this.size -=1;
    }
    return current && current.value;
  }

@yupeilin123 Can give an example where is behaving incorrectly?

I added some tests and it's working as expected. this.size - 1 is needed because the position starts with 0 and the size starts with 1.