Linked lists: singly, doubly and circular
About this lesson
A linked list stores each item in its own node, and each node holds a link to the next one. Build the list 3 → 7 → 9 in Python, walk it, add a node at the front, insert one in the middle and remove one, and watch that no node ever moves: only the links swing. Then give every node a second link for a doubly linked list, and point the end back at the start for a circular one. The Software lessons "Arrays and lists" and "Stacks and queues" cover the array side of the comparison.
A node: value plus link
A linked list keeps each item in its own small object called a node. A node holds two things: the value, here 3, and next, a reference (an arrow) to the node after it. Nodes can sit anywhere in memory.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
Node(3) makes one node:
value = 3
next = None until we link it
a reference is how Python names an object: an arrow, not a copyRead the lesson step by step
A node: value plus link
A linked list keeps each item in its own small object called a node. A node holds two things: the value, here 3, and next, a reference (an arrow) to the node after it. Nodes can sit anywhere in memory.
class Node: def __init__(self, value, next=None): self.value = value self.next = next Node(3) makes one node: value = 3 next = None until we link it a reference is how Python names an object: an arrow, not a copyFollow the links from head
head is a variable that refers to the first node. Each next points to the following node: 3, then 7, then 9. The last next is None, which marks the end. Python built this list with Node(3, Node(7, Node(9))).
head = Node(3, Node(7, Node(9))) head -> node 3 node 3.next -> node 7 node 7.next -> node 9 node 9.next -> None (the end) values(head) == [3, 7, 9]Walking to a value
There is no index to jump to. To find 7, node starts at head and follows next, one link at a time, until it holds 7. Reaching position k takes k hops: O(n) in the worst case, where a Python list index takes O(1).
1 node = head node -> 3 2 while node.value != 7: 3 != 7, go on 3 node = node.next node -> 7 (1 hop) 2 while node.value != 7: 7 == 7, stop position k needs k hops; the last of n items needs n - 1 get by position: O(n) worst case (array: O(1))Add at the front in O(1)
Node(1, head) makes a new node whose next is the old first node, 3. Then head moves to the new node. That is two link writes however long the list is: O(1). A Python list's insert(0, x) moves every item over: O(n).
4 head = Node(1, head) new node 1, its next -> node 3 (the old head) head -> node 1 values(head) == [1, 3, 7, 9] two writes for any length: O(1) list.insert(0, x) moves n items: O(n)Insert in the middle
To put 8 after 7, the new node first points at 7's old next, 9. Then 7's next moves to the new node. Nothing shifts in memory; two links change. Do it in this order, or 9 is lost.
5 new = Node(8, node.next) new.next -> 9 6 node.next = new 7.next -> 8 values(head) == [1, 3, 7, 8, 9] given node, inserting after it is O(1); finding node first was the O(n) walk wrong order: node.next = new first and nothing refers to 9 any moreUnlink a node
To remove 3, link around it: old holds node 3, then 1's next jumps over it to old.next, 7. No link in the list reaches 3 now, so once old is dropped Python can free it. No shifting, just one write.
7 old = head.next old -> node 3 8 head.next = old.next node 1.next -> node 7 values(head) == [1, 7, 8, 9] removing the node after one you hold: O(1) unreachable objects may be garbage-collectedDoubly linked: both ways
A doubly linked list gives each node a second link, prev, to the node before it. You can now walk backwards, and unlink a node you hold in O(1): node.prev.next = node.next, then node.next.prev = node.prev. The price: one more reference per node.
each node: prev | value | next 1.prev = None 9.next = None walk back from 9: [9, 8, 7, 1] def unlink(node): node.prev.next = node.next node.next.prev = node.prev unlink(node 8) -> [1, 7, 9], O(1)Circular: the end links back
Back to single links. In a circular list the last node's next points to the first instead of None. A walk never runs off the end, so a loop must stop when it is back at the start. It suits anything that takes turns.
tail.next = head node 9.next -> node 1 node = head while True: out.append(node.value) node = node.next if node is head: break out == [1, 7, 8, 9] (no None to stop at)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01head refers to the list 1 → 7 → 8 → 9. How many next links must you follow from head to reach the node holding 9?
9 is at position 3 (counting from 0), and reaching position k takes k hops: 1 → 7, 7 → 8, 8 → 9. There is no index to jump with.
02Why is adding a node at the front of a singly linked list O(1)?
head = Node(1, head) makes one node pointing at the old first node and moves head to it. No other node is touched. A Python list's insert(0, x) is O(n) because it moves every item.
03To insert new after node, which order of writes is correct?
The new node must take node's old next first. If you set node.next = new first, node.next no longer refers to the old next node, so new.next = node.next would point new at itself and the rest of the list would be lost.
04What does a doubly linked list add, and what does it cost?
With prev and next, node.prev.next = node.next and node.next.prev = node.prev remove a node in O(1). Getting the item at a position is still a walk.
05In a circular singly linked list, how does a loop that visits every node know when to stop?
The last node's next points to the first, so there is no None to hit. The loop stops when node is head again (if node is head: break).
Glossary
- Node
- A small object that holds one value and a link (a reference) to another node.
- Reference
- How Python points at an object without copying it. A node's next is a reference to the next node, drawn as an arrow.
- head
- The variable that refers to the first node. Lose it, and you lose the list.
- None
- Python's value for 'nothing'. The last node's next is None, which marks the end of a singly linked list.
- Singly linked list
- A list whose nodes each link to the next node only. You can walk it forwards, not backwards.
- Doubly linked list
- A list whose nodes link to both the next and the previous node, so you can walk either way.
- Circular linked list
- A list whose last node links back to the first instead of to None.
- Traversal
- Visiting the nodes one by one by following the links from head.
Notes and sources
- Costs follow MIT 6.006 (Lecture 2): a singly linked list inserts and deletes at the front in Θ(1) but needs O(n) to get or set the item at position i. Inserting or removing after a node you already hold is O(1); finding that node is the O(n) part.
- With only head, adding at the back means walking to the last node: O(n). Keeping a second variable, tail, makes adding at the back O(1). Removing the last node of a singly linked list is still O(n), because you need the node before it; a doubly linked list does it in O(1).
- Python's built-in list is an array of references, not a linked list: insert(0, x) and pop(0) move the other items, O(n). For fast adds and removes at both ends the standard library has collections.deque, which gives about O(1) appends and pops on either side.
- Every node pays for its links on top of its value: one reference in a singly linked list, two in a doubly linked one. Many implementations add dummy (sentinel) nodes at the ends so that inserting and deleting need no special case for an empty list.
- Python never destroys an object you ask it to; an object that nothing refers to any more may be garbage-collected. CPython frees most such objects as soon as the last reference goes.
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 2 notes: Data Structures (Linked List Sequence)
- Sedgewick and Wayne, Algorithms 4th ed., §1.3 Bags, Queues, and Stacks (linked lists)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), §10.2 Linked lists
- Python docs: collections.deque (and the O(n) cost of list.insert(0, v))
- Python tutorial: Classes (__init__ and instance attributes)
- Python Language Reference: Data model (objects and garbage collection)
Quiz Unlocks after the last step.