Linked list, visualized.
A list where every item knows just one thing: where the next one lives. Watch one land in memory, then wire, break and reverse one yourself.
What is a linked list?
A linked list is a sequence of nodes. Each node stores a value and a pointer to the next node, and the last node points to None. Unlike an array, the nodes can sit anywhere in memory: the pointers set the order, and your program keeps only the address of the first node, called the head.
This lesson builds a singly linked list, with one pointer per node. A doubly linked list adds a second pointer back to the previous node, and in a circular linked list the last node points back to the first instead of None.
01 — one item first
Start with one node.
Before a whole list, watch what happens to one number. Press Next and follow it into memory.
Python
02 — the trade
Linked list vs array.
An array keeps everyone in seats side by side. That is fast to read, but adding someone at the front means everyone has to shuffle along. A linked list just rewires one arrow.
Array
items moved: 0
Linked list
arrows changed: 0
Both hold 4, 8, 15, 16, 23. Now put 42 at the very front.
03 — your turn
Insert, delete and search.
Every button runs real code on the right, one line at a time. The ghost is cur: the only way to reach a node is to walk there, hop by hop.
Pick a value and press a button.
Python
04 — the classic bug
Lose a pointer, lose the list.
Nothing points to a node except other nodes. Forget one line and everything after it becomes unreachable. Python cleans it up; in C it leaks forever. Either way, your data is gone.
The list is 4 → 8 → 15. We want to add 1 at the front.
Buggy
Fixed
05 — your hands
Wire a linked list yourself.
Now you are the code. Click a node (or head) to pick whose arrow to move, then click the node it should point to. Order matters: let go of the only arrow to a node and it becomes a ghost.
06 — the boss level
Reverse a linked list.
The most asked linked list question in interviews. Three pointers walk the list and flip one arrow per lap. Watch nxt: for a moment it is the only thing holding the rest of the list.
Python
07 — cheat sheet
Linked list time complexity.
Use a linked list when you add and remove at the front a lot, or splice in the middle while you are already standing there. Use an array when you jump to item k or loop fast.
| Job | Array | Linked list |
|---|---|---|
| Read item k | O(1)jump straight to the seat | O(k)walk k hops from head |
| Add at the front | O(n)everyone shuffles along | O(1)one new node, one arrow |
| Add at the end | O(1)usually; sometimes it grows and copies | O(n)O(1) if you also keep a tail |
| Remove from the front | O(n) | O(1)head = head.next |
| Find a value | O(n) | O(n) |
| Extra memory | none | one pointer per itemand the boxes are scattered, which caches hate |
08 — take it with you
Linked list in Python: the full code.
Every function this page ran, in one file. Paste it into Python, build a list with push_front, and print it with find or a loop from head.
linked_list.py