In the remove method, we need to loop through the list until we get to the desired index (position). The code snippet to loop until the desired index is common in the LinkedList class methods. For this reason, we can refactor the code and extract the logic to a separate method so we can reuse it in different places. So, let's create the getElementAt method:
getElementAt(index) { if (index >= 0 && index <= this.count) { // {1} let node = this.head; // {2} for (let i = 0; i < index && node != null; i++) { // {3} node = node.next; } return node; // {4} } return undefined; // {5}}
Just to make sure we will loop through the list until we find a valid position, we need to verify whether ...