Data Structures Decoded: Singly Linked Lists V

Nicholas Echevarria
1 min readFeb 15, 2021
Photo by Jeff Kingma on Unsplash

Data structures: the other cornerstone of a competent software engineer. This is why I’m compiling all my research and work on the topic in a series of articles titled Data Structures Decoded.

Previously on Data Structures Decoded…

In the fourth part of our look at Singly Linked Lists, we created an .unshift() instance method.

In today’s article, we’ll be exploring how to add a .get() function to our Singly Linked List class.

Get A Clue

Let’s take a look at the pseudocode we’ll need to translate into Javascript:

  • The function should accept a value
  • If the index is less than zero or greater than the length of the list, return null
  • Loop through the list until you reach the index and return the node at that specific index

Get Your Implementation Right!

get(index) { 
if (index < 0 || index >= this.length) return null;
let counter = 0;
let current = this.head;
while(counter !== index) {
current = current.next;
counter++;
}
return current
}

What do you think? If you liked what you read, consider connecting or dropping me a line at nick.echev AT gmail.com!

--

--