A Beginner’s Guide to Linked Lists
By: Connor Finnegan
Tags: Coding, Programming, Arrays, Linked Lists, Data Structures

Did someone say Link?

As I’ve been preparing for interviews, I’ve been studying all of the major data structures and their various applications. Some, such as hashes and arrays, were already familiar to me through my coursework and projects at the Flatiron School and beyond. Others were less familiar or entirely new.

One of the more confusing data structures for me to wrap my head around at first was linked lists. They can be a little counterintuitive at first, particularly if you’re used to more flexible data structures like arrays and hashes. In this blog post, I’ll summarize my research of linked lists and share some of the examples and information that helped me wrap my head around them.

What Is a Linked List?

A linked list is a linear data structure that contains a sequence of nodes, in which each node contains data and a pointer to another node. The most common example is a singly linked list, where each node contains a piece of data and a pointer to the next list, as shown in the diagram below.

A singly linked list

This is one of the key distinguishing factors between a linked list and an array. Whereas we could access the value 37 if these numbers were in an array simply by pointing to the index, like so…

const arr = [12, 99, 37] console.log(arr[2]) // 37

…this is not possible in a linked list. To do so, we would have to traverse the linked list and continuing accessing the next value of each node until arriving at our desired value.

Another common version of a linked list is a doubly linked list, where each node also contains a reference to the previous node, as diagrammed below.

Advantages of Linked Lists

When I first encountered linked lists, I struggled to see their benefits. The thought of having to traverse the entire list to access an element seemed time-consuming, and I wondered what upside linked lists had to offer. What I hadn’t yet considered is the benefits linked lists offer when it comes to memory and storage.

Arrays are static in size, meaning the upper limit in terms of number of items has to be known in advance when creating the memory to store an array. This can often lead to extra memory that is not used because the upper limit is never reached, or, having to create a new object in memory if the upper limit is exceeded. This is less of an issue now because most modern programming languages have operations running under the hood that can efficiently shrink and grow arrays, but it remains a critical feature when it comes to understanding linked lists.

The other case where linked lists are very useful is random insertion of a new data point. If for example, you have an array [1, 3, 4, 5, 6, 7] and you want to insert the value 2 between 1 and 3, you would to have shift every value from 3 to 7 over a space so that 2 could be stored in the second position. This makes random insertion an expensive operation in terms of an memory when it comes to arrays. In a linked list, however, you could simply create a new node with the value data: 2 that points to the node with the value of 3 next, and update the next value of the first node to point to our new node instead of the one with the value of 3. This is a much cheaper operation than what we have to do with an array in this scenario.

Drawbacks of Linked Lists

As I previously mentioned, there are a few considerable drawbacks to linked lists.

Accessing a particular value of a linked list is much more costly in terms of time than performing the same operation on an array, due to the fact that you have to traverse the linked list one node at a time until you hit your desired value. This also means that you can’t perform even more time-efficient operations, such as binary search, on a linked list.

Also, if you know you’re going to be dealing with a static set of data, a linked list may not even be more efficient from a memory standpoint because you also have to use memory to store the next value of every node.

It’s important to understand these advantages and drawbacks when you’re deciding whether or not to use a linked list instead of other data structures, like arrays.