Linked List, step by step.

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 a linked list works

What is a linked list in data structures?

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.

Nodes, head and null

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.

Five nodes in a row, A, B, C, D and E, each drawn as a box with a data cell holding its letter and a next cell. An arrow runs from each node's next cell to the following node. A head label links to A, E's next is marked null, and one node's two cells are labeled data and next.
Each node holds its data and a next link: head refers to A, and E, the last node, has next = null.

Traversing and searching a linked list

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.

Inserting at the beginning of a linked list

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.

Insert at the beginning

All steps as text
  1. The list is B → D, and head refers to B. Step forward to insert A at the beginning.
  2. Suppose we have a linked list with two nodes (in fact, the size of the list does not matter to us) and we want to add one more node to the front of it.
  3. To do this, we first create a new node whose data field equals to, for example, A.
  4. We then assign the next field of the nodeA a reference to the first node: nodeA.next = head.
  5. Finally, we have to make the head field refer to the new node: head = nodeA. Insertion at the beginning is complete.
  6. The list reads A → B → D. No node was visited and only two links changed, so insertion at the beginning takes O(1).
Linked List — Insert at the beginning. Last step: The list reads A → B → D. No node was visited and only two links changed, so insertion at the beginning takes O(1).

No node is visited, whatever the size of the list, so insertion at the beginning takes O(1).

Inserting in the middle of a linked list: after a given node

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.

Insert in the middle

All steps as text
  1. The list is A → B → D, and head refers to A. Step forward to insert C after B.
  2. Now we want to insert a new node right after nodeB (note that "insertion in the middle" refers to the general concept of adding an element between existing nodes in a linked list. It means that the new node is inserted somewhere within the list, not at the beginning or the end).
  3. In the program code we use a variable called pred (short for predecessor) that initially refers to the first node. If the specified node is not nodeB, the variable refers to the next node in the list. This is repeated until nodeB is found or the end of the list is reached. Here we represent the pred variable as a marker.
  4. And so, the nodeB is found and we have access to it: pred = nodeB.
  5. Next, we create a new nodeC.
  6. Let the next field of nodeC refer to the same node as the next field of the node referenced by pred variable.
  7. All that remains for us is to set: pred.next = nodeC.
  8. The list reads A → B → C → D. Reaching B is a walk from the head, O(n) in general; changing the two links takes O(1).
Linked List — Insert in the middle. Last step: The list reads A → B → C → D. Reaching B is a walk from the head, O(n) in general; changing the two links takes O(1).

The walk to the predecessor costs O(n) and the two link changes O(1), so insertion in the middle is O(n).

Inserting at the end of a linked list (append)

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.

Deleting from a linked list

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.

Delete from the middle

All steps as text
  1. The list is A → B → C → D, and head refers to A. Step forward to delete C.
  2. Let's now remove nodeC (note that "deletion in the middle" refers to the general concept of removing an element from somewhere within a linked list, not from the beginning or the end).
  3. Two variables, shown as markers, walk the list together: pred (short for predecessor) starts at A, and cur (short for current) at B. Both move right until cur refers to a node for which the condition data = C is true.
  4. The nodeC is found.
  5. We are changing the next field of the node referenced by pred variable: pred.next = cur.next.
  6. Since there are no links to nodeC, this node is considered removed from the list.
  7. The list reads A → B → D. Reaching C is a walk from the head, O(n) in general; changing one link takes O(1).
Linked List — Delete from the middle. Last step: The list reads A → B → D. Reaching C is a walk from the head, O(n) in general; changing one link takes O(1).

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.

Why is access by index slower in a linked list than in an array?

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.

How do you delete a node from a linked list?

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).

Linked list time complexity

OperationHead onlyHead and tail (double-ended)
Access by indexO(n); the first node O(1)O(n); the first and last nodes O(1)
SearchO(n)O(n)
Insert or delete at the beginningO(1)O(1)
Insert or delete in the middleO(n)O(n)
Insert at the endO(n)O(1)
Delete at the endO(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.

Linked list vs array

PropertyLinked listArray
Access by indexO(n)O(1)
Insert or delete at the frontO(1)O(n)
Insert or delete in the middleO(n)O(n)
Insert or delete at the endO(n); insert O(1) with a tailO(1), amortized for a dynamic array
MemoryEach value with one linkThe values, plus any spare capacity
LayoutNodes scattered in memoryContiguous, 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.

Types of linked list: singly, doubly and circular

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.

Double-ended linked list (head and tail)

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.

Four nodes in a row, A, B, C and D, each drawn as a box with a data cell and a next cell, joined by arrows from each node to the following one. A head label links to A, and a tail label links to D.
A double-ended list keeps two links into the list: head to the first node, A, and tail to the last, D.

Linked lists in Python, Java and C++

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++.

Where linked lists are used: advantages and disadvantages

  • Stacks: push and pop at the head, both O(1).
  • Queues: a double-ended list adds at the tail and removes at the head, both O(1).
  • Hash tables with separate chaining keep each bucket's entries in a linked list.
  • An adjacency list can keep each vertex's neighbors in a linked list.

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

Three animations here.
The whole topic 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.

Nodes, printing, access speed

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.

Both ends, a recap and a tail

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).

Forward and back

Step through every slide and step back, or jump to any slide from the list. The show reopens where you left it.

Theory and complexity

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.

Python, Java and C++

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.

Quiz

Ten questions to check what you have learned before moving on.

Build a linked list
node by node.

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