Skip to the content.

Linked Lists

img

Traversing

As you traverse a linked list, you cannot use a forEach or a for loop. We are relying on the Next value to guide the node to the next reference point. A next property is exceptionally important because it will lead us to the next node and allows us to extract data appropriately.

So what can you use to traverse?? The best approach is to use a while() loop. This will allow us to continually check that the next node is the list is not null.

Example

ALGORITHM Includes (value) // INPUT <– integer value // OUTPUT <– boolean

Current <– Head

WHILE Current is not NULL IF Current.Value is equal to value return TRUE

Current <– Current.Next return FALSE

What took place

1. We first create Current at the Head to guarantee we are starting from the beginning.

2. We create the while loop and this will only run is the node that Current is pointing too is not null.

3. Once we are in the while loop, we are checking if the value of the current node is equal to the value that we were looking for.

4. If the Current node does not contain the value we are looking for, we must move the current to the next node that is being referenced.

5.At this point, the while loop is run again and steps 3 & 4 will continue until Current reaches the end of the LinkedList.

Adding a Node

Prerequisites

When constructing your code, keep in mind that a Node class will be passed in to each node that has a value.

img

When constructing your code, a few things to keep in mind.

What’s a Linked List, pt1

What’s a Linked List, pt2