2) Data Structures Lesson

What is a Linked List?

10 min to complete · By Jon Fincher

You can start the discussion of abstract data structures with the versatile and simple linked list.

What is a Linked List?

A linked list is an abstract data structure that stores information much like an array. A linked list consists of a number of items, each of which contains at least two pieces of information:

  • The data being stored
  • A link or reference to the next item in the list

The items in the linked list are usually called nodes. Sometimes, a picture can help with understanding -- it's time to take a look at the visualization of a linked list:

A Linked List visualized

In this picture, each large block represents a node. The node contains the data and a link (sometimes called a reference or pointer) to the next node.

The first node in the list is called the head, and it's where you start when working with a linked list. The last item in the list (often called the tail) doesn't have any more nodes to reference, so its next link is set to null or Nothing.

Why use a Linked List

Linked lists are often used to store sequential data, especially when you don't know how much data you will need to store. For this reason, they are often used as a replacement for arrays. Like arrays, they keep data in sequential order. However, because they can grow and shrink depending on how much data is kept in them, they can be a better choice for data sets of unknown size.

Because the size of the linked list can grow and shrink as you add and remove items from the list, it is often referred to as a dynamic data structure.

Use cases for a Linked List

Linked lists are widely used for two main reasons:

  • You need to store data but don't know exactly how much to store.
  • You have to access data sequentially, either add it to the list or process it later.

For example, assume you are reading data from a text file. You want to store all the capitalized words in a separate file. Since you read the text file sequentially and don't know how many capitalized words there are, a linked list could be a good option for storing the words. You can store each capitalized word in a linked list as it appears in the text, preserving the order.

Benefits of Linked Lists

The dynamic nature of the linked list means there are no size restrictions on them. You can add data to and remove data from a linked list at will.

Linked lists are often the basis for other data structures. You will learn about stacks and queues in a later unit, which often uses a linked list to store and retrieve their data.

Unlike arrays, linked lists do not normally allow indexed or random access to the data they contain. To get to any item in the list, you have to start at the head and traverse the list. This can have an impact on performance.

Modification to Linked Lists

There are modifications to the standard linked list that help overcome some of their deficits:

  • Adding data to the tail of a list is made easier by storing and maintaining a reference to the tail.
  • Doubly-linked lists have references to both the next and previous nodes in the list, making traversal easier.
  • Circularly linked lists have the tail reference the head of the list, effectively turning the list into a cycle.
  • Skip lists maintain a second next pointer, which refers to a node two, three, or even four items away from the current node. This allows quicker searches through the list.

Note that all these modifications also complicate other operations, notably insertions into and deletions from the list.

Big O Analysis

Operation Average Worst case
Search $`O(n)`$ $`O(n)`$
Insert $`O(n)`$ $`O(n)`$
Delete $`O(n)`$ $`O(n)`$

Adding, deleting, or finding an item in a linked list depends on where the item is. If you are working at the head of the list, then the time is constant since that item is always known. However, if you are working at the tail of the list, then the time of any operation depends on the size of the list. If a list contains $`n`$ items, the average of the best and worst is $`n/2`$. However, remember you ignore constant factors in your complexity analysis, so you can classify all operations as $`O(n)`$.

Colorful illustration of a light bulb

If you'd like to continue learning about Linked Lists, check out the following resources in the Java 301 course:

Terminology

Below is the terminology used when talking about linked lists.

  • Data in a linked list is stored in a node.
  • The first node in any linked list is called the head.
  • Each node contains a reference to the next node in the list, called a link.
  • You can usually only move from the head to the end of the list in one direction.
  • The last node in the list is called the tail.
  • Since there are no nodes after the tail, its reference is usually set to null or Nothing.

Summary: Linked Lists

You explored the concept of a linked list. You learned that a linked list stores data in a dynamic way -- nodes are created to store the data, and they are linked to other nodes. Each node has a next reference, which points to the next node in the list. There is a special first node in the list called the head, and the last node in the list is the tail. Linked lists can replace arrays when the amount of data being stored is unknown, as they can be of variable size as the program runs.