Linked lists

Published

2023-08-05

A linked list is a data structure consisting of a collection of one or more nodes. Each node (apart from the last node in the list) includes a pointer to the next node in the list. In this way, we can traverse a linked list by hopping from node to node, following the pointers.

Schematic of a linked list, showing nodes and pointers

A linked list is much like a bicycle chain. In a bicycle chain, each set of outer plates (corresponding to a node) is joined to the next set in the chain by a pair of inner plates and a pin (corresponding to a pointer). Unlike a bicycle chain, most linked lists do not form a loop—they have a beginning and an end. Typically, there will be some indicator at the end of a list to mark it as the end.

Each node in a linked list contains at least two elements: some data and a pointer to the next node.

Linked lists are convenient since they can be expanded dynamically by adding a new node and pointer. Nodes can be removed easily, and the chain can be preserved by modifying the dangling pointer. The downside of linked lists is that they must be traversed in a linear fashion. To get to the nth element, one must start at the first element and traverse the list until the nth element is reached. (QUICK: What’s the 17th letter of the alphabet?)

We will implement a node class for constructing singly-linked lists (where links point to successors in the chain).

Additional reading

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.