Singly Linked List Series: Creating insert(), remove(), and reverse() methods with JavaScript
Published: 2022-12-17 16:29:50

I will be wrapping up this Singly Linked List Series by sharing my understanding of creating insert(), remove(), and reverse() methods.  Beginning with insert(), this method receives two parameters: index and value.  This method adds a node at a specified index with the value passed.  It returns true if the node was inserted; otherwise, it returns false.  This method uses [get()](https://dev.to/tartope/singly-linked-list-series-creating-get-and-set-methods-with-javascript-3o51), [unshift() and shift()](https://dev.to/tartope/singly-linked-list-series-creating-shift-and-unshift-methods-with-javascript-2j37), and [push() and pop()](https://dev.to/tartope/singly-linked-list-series-creating-push-and-pop-methods-35n4) methods to support it.  See the previous Singly Linked List Series blog posts linked above to get an understanding of creating these methods.  Let's start with a visualization for creating this method:

![Drawing of insert method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cp5ioqcu78wu7wd6p4d4.png)

Edge case 1: if the index is a negative number or greater than the length of the list, you can return false because that index is not valid.
Edge case 2: if the index is equal to zero, you can use the unshift() method to place a new node at the beginning of the list.
Edge case 3: if the index is equal to the length of the list, you can use the push() method to place a new node at the end of the list.
If none of the above edge cases apply, you can follow the steps above.

Here is an example of code for an insert() method:

![Image of code for an insert method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwmht3jhvjxr187yu6de.png)

A remove() method will remove a node from a Singly Linked List at a specific index.  This method accepts an index as a parameter.  The visualization looks like this:

![Drawing of remove method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0q9907fcbx13vxnh5zkx.png)

Edge case 1: if the index is a negative number or greater than or equal to the length, you can return undefined because that index is not valid.
Edge case 2: if the index is equal to zero, you can use the shift() method to remove a node from the beginning of the list.
Edge case 3: if the index is equal to the last index in the list, you can use the pop() method to remove a node from the end of the list.
Same as before, if none of the above edge cases apply, you can follow the steps above.

Here is an example of code for a remove() method:

![Image of code for a remove method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6nqty20zl7ikr3gwpc9.png)

Lastly, the reverse() method that I will share reverses the list in place.  This method takes no parameters.  See the steps and visualization below:

![Drawing of reverse method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ay0ea1qy6lzck3h713js.png)

![Drawing of reverse method continued](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5f9i1ls0i0f9ot1ifonb.png)

Here is an example of code for a reverse() method:

![Image of code for a reverse method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qi0utqynjina6f4heoy.png)

The time complexity of these insert(), remove(), and reverse() methods for a Singly Linked List are O(n) linear time because the number of operations increases as the lists' length increases.

Writing out the steps and drawing the visualizations really helped me better my understanding of creating methods for Singly Linked Lists, and my hope is that this is helpful to someone else, too.  However, there is still so much more for me to learn, so feel free to share your comments or corrections if there is anything written above that is incorrect. 🙂