A linked list is a data structure that stores a sequence of elements in nodes, each holding its data and a link to the next node. The nodes can sit anywhere in memory, so a node is reached by following links from the head: the first in O(1) time, any other in O(n). This page explains how it works, with step-by-step animations of insertion at the beginning, insertion in the middle and deletion in the middle from the VisiGrab app.
// how linked lists work
A linked list is a linear data structure that stores a sequence of elements called nodes. Each node holds its data and a link to the next node. The list is reached through its head, a link to the first node, and the last node's next link is null. Unlike an array's elements, the nodes need not sit side by side in memory: the links alone keep their order.
A node has two fields. data holds the value, of any type. In the app it is one letter, and each node is named after it: nodeA holds A. next is the link to the following node, or null in the last node (None in Python, nullptr in C++). The list itself keeps one field, head, the link to the first node; it is null when the list is empty. Every operation starts from head: without it, the list cannot be reached. A list with one next link per node and a head is a singly linked list. The app builds this picture field by field, next to the node's pseudocode.
Note
In C and C++ a link is a pointer; in Java and Python it is a reference.
To traverse a list, start at head and follow the next links until one is null. Each node links only to the next, so a singly linked list is walked forward only. Even reading the last node means starting from the first. The first node is reached in O(1), but the node at index k takes k steps: access by index is O(n). This is sequential access; an array gives random access. Search is the same walk, O(n), even in a sorted list: binary search needs a quick way to the middle, and a list has none. A binary search tree built from sorted keys degenerates into such a chain. The app shows a print walk from A to E, then a speedometer for the cost of access.
Insertion at the beginning takes three steps: create the node, set node.next = head, then set head = node. The order matters: moving head first would leave nothing linked to the old first node, and the rest of the list would be lost. In the player, A is inserted in front of B → D.
No node is visited, whatever the size of the list, so insertion at the beginning takes O(1).
Insertion in the middle means anywhere but the beginning or the end. The app's code inserts after the node holding a given value. A variable pred, short for predecessor and often called prev, starts at the first node and follows the next links to that node. Then node.next = pred.next, and only then pred.next = node: the other order would lose the rest of the list. In the player, C goes after B in A → B → D.
The walk to the predecessor costs O(n) and the two link changes O(1), so insertion in the middle is O(n).
To append, pred walks from the first node to the last, whose next is null, and sets pred.next = node. The walk makes insertion at the end O(n); with a tail it is O(1). The app shows it step by step.
A node is deleted by changing the link that refers to it. Deletion in the middle uses two variables: pred and cur, short for current, one node ahead of it. They move on together until cur reaches the node to delete; then pred.next = cur.next. In the player, C is deleted from A → B → C → D.
Deletion at the beginning needs no walk: head = head.next, in O(1). Deletion at the end walks pred and cur until cur's next is null, then sets pred.next = null, in O(n). The app shows both step by step.
Note
In C++ a removed node must also be freed with delete, or its memory leaks. Java and Python reclaim it automatically once nothing refers to it.
The nodes can sit anywhere in memory, so the node at index k is reached only by following k links from the head: O(n). An array computes any element's address from its index, in O(1). Linked list vs array compares the other operations.
Make the link that refers to the node skip over it: head = head.next for the first node, pred.next = cur.next for any other node cur, where pred is the node before it. The relink takes O(1), but finding pred is a walk from the head, O(n).
| Operation | Head only | Head and tail (double-ended) |
|---|---|---|
| Access by index | O(n); the first node O(1) | O(n); the first and last nodes O(1) |
| Search | O(n) | O(n) |
| Insert or delete at the beginning | O(1) | O(1) |
| Insert or delete in the middle | O(n) | O(n) |
| Insert at the end | O(n) | O(1) |
| Delete at the end | O(n) | O(n) |
Insertion is O(1) only at the beginning, right after a node you already hold, or at the end with a tail. Deletion is O(1) at the beginning, and so is removing the node after one you hold. Removing the held node itself needs its predecessor, and only a walk from head finds it. Space is O(n), with one extra link per node.
| Property | Linked list | Array |
|---|---|---|
| Access by index | O(n) | O(1) |
| Insert or delete at the front | O(1) | O(n) |
| Insert or delete in the middle | O(n) | O(n) |
| Insert or delete at the end | O(n); insert O(1) with a tail | O(1), amortized for a dynamic array |
| Memory | Each value with one link | The values, plus any spare capacity |
| Layout | Nodes scattered in memory | Contiguous, cache-friendly |
A linked list wins at the front, where an array must shift every element. An array wins at the end and for access by index. In the middle both are O(n), but arrays are often faster in practice: shifting contiguous memory beats following scattered links.
A singly linked list has one next link per node and is walked forward only. A doubly linked list adds a prev link to the previous node, so it can be walked both ways, at the cost of a second link per node. In a circular linked list the last node links back to the first, so no next link is null. The app covers the doubly linked list as a separate topic.
A double-ended linked list keeps a second link, tail, to the last node, while head still refers to the first. It is not a doubly linked list: each node still has only a next link. With a tail, insertion at the end needs no walk: tail.next = node, then tail = node, in O(1). Deletion at the end stays O(n). The second-to-last node must get next = null and become the tail. With no backward links, only a walk from head finds it, stopping where pred.next = tail. The app shows both step by step.
Java's java.util.LinkedList is a doubly linked list. In C++, std::forward_list is singly linked and std::list doubly linked. Python has no built-in linked list; the nearest is collections.deque, built from linked blocks of items rather than one node per item.
The full source code is on GitHub, in Python, Java and C++.
O(1).O(1).Advantages: O(1) insert and delete at the beginning, and growth one node at a time, with no shifting or resizing. Disadvantages: no direct access by index (reaching the k-th node is a walk, O(n)), one extra link per node, poor cache locality and, in a singly linked list, forward-only traversal.
6:57
Video
Linked Lists Explained | Neo & Byte Ep. 1
Nodes and links, inserting and deleting at the beginning, middle and end, and linked lists vs arrays.
Watch on YouTube
// in the app
In VisiGrab the linked list is a 73-slide show, from what a node is to the double-ended list, played one step at a time.
The node's pseudocode, with its data and next fields traced on the list node by node, then a print walk from A to E. A speedometer moves from O(1) for the first node to O(n) for any other, and the list is compared with an array.
Insertion at the end, and deletion at the end and at the beginning. A recap gives the list a winner's cup at the beginning and the array one at the end. Then a tail makes insertion at the end O(1), while deletion there stays O(n).
Step through every slide and step back, or jump to any slide from the list. The show reopens where you left it.
Two tabs: Info, with the key characteristics, advantages and limitations, and Efficiency, with a complexity table for the singly and the double-ended list. Tips explain links and the head.
A linked list with six insert and delete methods, in the order the slide show plays them, then a double-ended list's insertLast and deleteLast, explained part by part.
Ten questions to check what you have learned before moving on.
// build your own
Download VisiGrab and step through traversal, insertion, deletion and the double-ended list yourself.
The linked list is one of 30+ topics. See them all