Linked Lists Explained From Scratch

Abstract generated cover for Linked Lists Explained From Scratch.

A linked list is a data structure made of nodes. Each node stores a value and a reference to the next node. Instead of storing items next to each other by index like an array, a linked list connects items together with links. That is why it is called a linked list. Linked lists are not something you use every day in Python application code, but they are worth learning because they teach important ideas:

  • references
  • traversal
  • insertion
  • deletion
  • pointer-style thinking
  • tradeoffs between data structures They also show up inside more advanced structures and algorithms.

The Mental Model

Think of a treasure hunt. Each clue tells you where to find the next clue. You do not have a numbered index that lets you jump directly to clue 7. You start at the first clue and follow the chain. A linked list works like that. ```plain text head -> [10] -> [20] -> [30] -> None

The first node is called the head.
Each node points to the next node.
The final node points to \`None\`, meaning the list is over.
## A Basic Node in Python
Here is a simple linked list node:
```python
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

We can manually connect nodes:

first = Node(10)
second = Node(20)
third = Node(30)

first.next = second
second.next = third

Now we have: ```plain text first -> second -> third -> None

Or by values:
```plain text
10 -> 20 -> 30 -> None

Traversing a Linked List

To read a linked list, start at the head and follow `next`.

def print_list(head):
    current = head

    while current is not None:
        print(current.value)
        current = current.next

Usage:

print_list(first)

Output: ```plain text 10 20 30

Traversal is O(n) because you may need to visit every node.
## Why Not Just Use an Array?
Arrays and linked lists have different strengths.
With an array, you can access an item by index quickly:
```python
items[3]

With a linked list, you cannot jump directly to the fourth node. You have to start at the head and follow links. That makes indexed access slower. But linked lists can be useful when you already have a reference to a node and need to insert or remove nearby nodes without shifting a whole array.

Inserting After a Node

Suppose we have: ```plain text 10 -> 20 -> 30

And we want to insert \`25\` after \`20\`.
```python
new_node = Node(25)

new_node.next = second.next
second.next = new_node

Now the list is: ```plain text 10 -> 20 -> 25 -> 30

The key is the order:
1. Point the new node at what used to come after \`second\`.
2. Point \`second\` at the new node.
If you reverse the order carelessly, you can lose part of the list.
## Removing a Node
To remove a node, you change the previous node's \`next\` reference.
If we have:
```plain text
10 -> 20 -> 25 -> 30

And want to remove `25`:

second.next = second.next.next

Now `20` points directly to `30`. The `25` node is no longer part of the chain.

Common Mistakes

Mistake 1: Losing the rest of the list

When changing links, order matters. This can break the chain:

current.next = new_node
new_node.next = current.next

After the first line, `current.next` no longer points to the old next node. You lost the reference. Save references before overwriting them.

Mistake 2: Forgetting the head can change

If you insert or remove at the beginning, the head may need to change.

head = head.next

Head changes are a common source of linked list bugs.

Mistake 3: Thinking linked lists are always better for insertion

Linked lists are only efficient for insertion or deletion when you already have the relevant node reference. If you have to search from the head first, that search is still O(n).

Where This Shows Up in Real Projects

You may not often build linked lists manually in Python web apps, but the ideas appear in:

  • browser history
  • undo/redo systems
  • LRU caches
  • memory management concepts
  • graph structures
  • queues and deques
  • low-level systems programming Linked lists are also a useful training ground for thinking carefully about references. That makes them valuable even if you mostly use higher-level data structures in day-to-day code.

Key Takeaways

  • A linked list is made of nodes.
  • Each node stores a value and a reference to the next node.
  • Linked lists are traversed from the head.
  • Indexed access is slower than arrays.
  • Insertions and deletions are mostly about carefully changing links.
  • Linked lists are useful for learning reference-based thinking.

    Related Articles

  • Arrays Explained: Indexing, Searching, and Updating

  • Stacks Explained With Real Examples
  • Queues Explained With Real Examples

← Back to Blog Index