Data Structures: Singly Linked List Part Four

Get and Set

Hello world, in this blog post I will continue breaking down the Singly Linked List Data Structure, specifically the get and set methods. If you don’t know what a Linked List is or what I mean when I say that we are going to be looking at new methods for a linked list, feel free to go back and read some of my previous blogs.
Part One: Singly Linked Lists and the Push Method
Part Two: Singly Linked Lists and the Pop Method
Part Three: Singly Linked Lists and the Shift and Unshift Method

What is Get?

Get is a method that returns an item from a given position inside of the linked list. So if we had a list of 10 items and we wanted to get the third item, this is the method we would write in order to do so. This works similarly to getting items in an array, in that we start with the 0th position with an input of 0 to then get and return the first item or the Head of our linked list. And in this example, we would use 9 as the input to get/return the 10th/last item/tail in our list. To get any item, we need to iterate through the list hitting .next in each of the nodes until we get to the numbered position; the exception being in the case of 0 which there is no need to hit .next. So unlike an array where there is a built in index, we need to manually count up to the desired item we are getting.

Writing out the get method

When writing the get method, the function accepts a number as its argument to represent the index. For our edge case, if the index is less than 0 or greater than or equal to the length of the list, we will return null. And then we will create a loop that will go through the list until it reaches the index and then it will return the node at that index. To keep track, of our traversal, we will create a counter variable that will start at zero and will increase every time we hit the .next inside each node.

What is Set?

Set is similar to get, in that we will be traversing the linked list given an index, however, here is how it is different. A set method accepts two arguments: (1) the index we want to change and (2) the new value we will insert and replace with that index. So for example let’s say we had a linked list that was very simple and had the values 1 through 5 written out. 1 would be in the index of 0 and the value 5 will be in the index of 4. And let’s say we wanted to replace the item in index 2 with a word like “hello”. Our starting list will look like:
1 => 2 => 3 => 4 => 5
and the set method would look like:
set(2, “hello”)
and this would return a list that looks like:
1 => 2 => “hello” => 4 => 5

Writing out the set method

When writing the set method, the function will accept an index and a value. Next we will use the existing get method to find the specific node. An edge case for this is that if we do not find the node, we return false, meaning we can’t change the value of an index that doesn’t exist. This will prevent our code breaking if someone accidentally or purposefully enter a number that is greater than the length of our list, or less than 0. If the node is found, we will update the value of the node to equal the passed in value from the set method and return true.

Below is the full code with everything we’ve worked out until this point:

A Software Engineer with a background in Education Technology and Dance. Recent grad form FlatIron Bootcamp, and passion for the arts and working with databases

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