Implementing a Singly-Linked List in JavaScript pt. 2: Advanced Methods

If you missed it catchup with the code on the first blog I wrote about singly-linked lists in JavaScript. I won’t be doing a recap about the code we have so far so if you are lost please check it out!

We will now start implementing some more advanced methods to our linked-list. Let’s take a look at a get/1 method.

Image for post
Image for post
get/1 method

Here we take a desired index, an integer value, and check for edge cases. We make sure the number exists within the range of our list and if it does not, return undefined.

Next we create a counter variable and set it to 0. We also create a node variable and set it equal to the head of the list. We go from node to node, using the next property of the node variable, updating the node variable as we go. We also increment the counter as we iterate through the nodes.

Finally, when the counter variable is equal to the desired index, we return the node variable, which should be set to the desired node. Pretty straightforward!

Now we have set/2.

Image for post
Image for post
set/2 method

We take advantage of our newly built get method and use it to return the node at the index we want to update. We check to see that a node was properly returned (that the index value was within the range of values of the linked list) and then set the value of the node to the new value we want. We exit the code block by returning true if the operation succeeded and false if it did not.

The last method I will talk about in this update on my data structures studies is the reverse/0 method.

Image for post
Image for post
reverse/0 method

First, we define a node variable and set it to the head. We do this so we can store the value of the current head of the list.

Next, we set the value of the list’s head, to the value of its tail. This is where we can now being reversing the list.

The tail of the list gets set to the previous head value we stored in the node variable. So now that our first and last values are properly updated, we have to change the values in between them.

We sat a variable called previous equal to null and a next variable that we do not yet assign. We check each value of the list, and set the next variable equal to the next property of the node variable. In the first pass through, the node variable is still holding the original head value. Since we are working our way from the back of the list to the front, the new tail’s (the old head) next property gets set to the value of previous (in the first pass through this must be null as the tail of a linked list must have a next property of null).

Finally, we swap the value of previous to the value of the current node and the value of node to the value of next (which in the first pass through is the second element in the list).

After it completes every iteration we return this. This in this case (ha) refers to the list itself.

In my next installation in this series I plan on covering insert and remove methods as well as how we can utilize the basic structure of a linked list to implement more advanced data structures like stacks and queues.

Full-Stack Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store