Sub topic 6
Closing the loop: the circular linked list
This chapter assumes you can write a Python class with methods and generators, and that you know how a singly linked list stores a next reference. It also assumes you know Big-O notation and that None marks "no node". The chapter covers what a circular list is and why it stores a tail reference. The next chapter covers its operations in more detail.
What changes in a circular list
In a singly linked list, the last node has next = None. In a circular linked list, the last node's next points back to the first node. No node holds None, so the nodes form a ring.
This changes two things:
- The idea of a "last node" is now only a convention. Any node can reach every other node by following
next. - The usual stop condition,
while node is not None, never becomes false. A loop written that way runs forever.
Why store the tail, not the head
A plain singly linked list usually keeps a head. A circular list works better with a tail reference only, because the head is always tail.next. One reference then gives O(1) access to both ends.
| Stored reference | Reach head | Reach tail | Insert at front | Insert at back |
|---|---|---|---|---|
| `head` only | O(1) | O(n) | O(n), must find the tail to relink it | O(n) |
| `tail` only | O(1) via `tail.next` | O(1) | O(1) | O(1) |
With only a head, adding at the back means walking around to find the node that links to the head. With a tail, both insertions take a constant number of pointer changes.
Building it
The code below is a complete circular list with one tail reference.
class SNode:
def __init__(self, value):
self.value = value
self.next = None
class CircularList:
"""Circular singly linked list that stores only a tail reference."""
def __init__(self):
self.tail = None
def push_front(self, value):
node = SNode(value)
if self.tail is None:
node.next = node
self.tail = node
else:
node.next = self.tail.next
self.tail.next = node
def push_back(self, value):
self.push_front(value)
self.tail = self.tail.next
def rotate(self):
if self.tail is not None:
self.tail = self.tail.next
def __iter__(self):
if self.tail is None:
return
node = self.tail.next
while True:
yield node.value
if node is self.tail:
break
node = node.next
lst = CircularList()
for v in (1, 2, 3):
lst.push_back(v)
lst.push_front(0)
print(list(lst)) # [0, 1, 2, 3]
lst.rotate()
print(list(lst)) # [1, 2, 3, 0]How the pieces work
The empty case. When the list is empty, the new node must point to itself. A one-element circular list is a ring of length one: node.next is node. If you leave next as None, the ring is broken and the next insertion fails with an AttributeError.
Insertion at the front. The new node copies the old head (tail.next) into its own next. Then the tail points to the new node. The tail does not move, so the new node becomes the head.
Insertion at the back. push_back reuses push_front and then moves tail forward by one. The new node was inserted just after the tail, so advancing the tail makes it the last node. The ring itself is unchanged. Only the label "tail" moves.
Rotation. rotate moves the tail one step forward. The old head becomes the new tail, and the second node becomes the head. This costs O(1) and moves no nodes.
Traversal. The generator starts at tail.next, the head. It yields each value and stops after yielding the tail. It tests for the tail after yielding, so a one-element list still yields its single value. Testing before yielding would skip a value.
Exercise
A CircularList holds 5, 6, 7 in that order, and tail is the node with value 7. After calling push_back(8) and then rotate(), what does list(lst) return?
Show answer
[6, 7, 8, 5]. After push_back(8) the list is [5, 6, 7, 8] with the tail at 8. rotate() moves the tail to the node after 8, which is 5. The head becomes 6, so iteration yields 6, 7, 8, 5.