Refactoring the remove method

We can refactor the remove method and use the getElementAt method that has been created. To do so, we can replace lines {4} to {8} as follows:

if (index === 0) {  // logic for first position} else {  const previous = this.getElementAt(index - 1);  current = previous.next;  previous.next = current.next;}this.count--; // {9}

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.