Data structures

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

class Node:
def __init__(self, value):
self.value = value
self.next = None # nothing after it yet
head = Node(7)
head.next = Node(3)
head.next.next = Node(9)

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

def push_front(head, value):
node = Node(value)
node.next = head
return node # new head
Each operation ends with its cost here.

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

node = Node(1)
head = node # forgot node.next = head

Fixed

node = Node(1)
node.next = head # hold on to the old list first
head = node

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

def reverse(head):
prev = None
cur = head
while cur is not None:
nxt = cur.next # save the rest
cur.next = prev # flip one arrow
prev = cur
cur = nxt
return prev # the new head
Time O(n): one lap per node. Extra memory O(1): just three pointers, no new nodes.

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.

JobArrayLinked list
Read item kO(1)jump straight to the seatO(k)walk k hops from head
Add at the frontO(n)everyone shuffles alongO(1)one new node, one arrow
Add at the endO(1)usually; sometimes it grows and copiesO(n)O(1) if you also keep a tail
Remove from the frontO(n)O(1)head = head.next
Find a valueO(n)O(n)
Extra memorynoneone 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

class Node:
def __init__(self, value):
self.value = value
self.next = None # nothing after it yet
def push_front(head, value):
node = Node(value)
node.next = head
return node # new head
def append(head, value):
node = Node(value)
if head is None:
return node
cur = head
while cur.next is not None:
cur = cur.next # one hop
cur.next = node
return head
def find(head, value):
cur = head
while cur is not None:
if cur.value == value:
return cur
cur = cur.next # one hop
return None
def remove(head, value):
if head is None:
return None
if head.value == value:
return head.next # skip the first
prev = head
while prev.next is not None:
if prev.next.value == value:
prev.next = prev.next.next
return head
prev = prev.next # one hop
return head
def insert_after(head, target, value):
cur = head
while cur is not None and cur.value != target:
cur = cur.next # one hop
if cur is None:
return head
node = Node(value)
node.next = cur.next # 1. grab the rest
cur.next = node # 2. then link in
return head
def reverse(head):
prev = None
cur = head
while cur is not None:
nxt = cur.next # save the rest
cur.next = prev # flip one arrow
prev = cur
cur = nxt
return prev # the new head
Up nextStacks and queues