Sub topic 10
Comparing cost and memory across list variants
This chapter assumes you have read the earlier chapters on singly, doubly, and circular lists. It also assumes you know Big-O notation and basic Python classes. Here you compare the variants on two things: how many steps each operation takes and how much memory each node uses.
Why the comparison matters
The variants differ in two ways. An extra pointer costs memory in every node. A tail reference or a back pointer changes which operations can avoid walking the list. The question to ask about any operation is whether it must find a predecessor or the last node.
Operation costs
The table assumes the setups from earlier chapters. "Singly" keeps only a head. "Doubly" keeps a head and a tail. "Circular singly" keeps only a tail. "Circular doubly" uses a sentinel node.
| Operation | Singly | Doubly | Circular singly | Circular doubly |
|---|---|---|---|---|
| Push front | O(1) | O(1) | O(1) | O(1) |
| Push back | O(n) | O(1) | O(1) | O(1) |
| Pop front | O(1) | O(1) | O(1) | O(1) |
| Pop back | O(n) | O(1) | O(n) | O(1) |
| Insert after a node you hold | O(1) | O(1) | O(1) | O(1) |
| Remove a node you hold | O(n) | O(1) | O(n) | O(1) |
| Search by value | O(n) | O(n) | O(n) | O(n) |
| Access by index | O(n) | O(n) | O(n) | O(n) |
Two rows show the real differences:
- Pop back. Removing the last node means updating the node before it. With only
nextpointers, you must walk from the front to find that node. Aprevpointer gives it to you directly. - Remove a node you hold. The same reasoning applies. A singly linked list has to search for the predecessor first.
Searching and indexing stay O(n) in every variant. No list variant changes that.
Counting the hops
This code counts how many pointer hops each design needs to reach the predecessor of the last node.
class SNode:
__slots__ = ("value", "next")
def __init__(self, value):
self.value = value
self.next = None
class DNode:
__slots__ = ("value", "prev", "next")
def __init__(self, value):
self.value = value
self.prev = None
self.next = None
def singly_hops(head, target):
"""Hops needed to reach the node before target."""
hops = 0
node = head
while node.next is not target:
node = node.next
hops += 1
return hops
def build_singly(n):
head = SNode(0)
node = head
for i in range(1, n):
node.next = SNode(i)
node = node.next
return head, node
for n in (10, 100, 1000):
head, last = build_singly(n)
print(n, "singly hops:", singly_hops(head, last), "doubly hops: 0")The singly count grows with the list length. The doubly count is always 0, because last.prev is the predecessor.
Memory overhead
Each node stores a value plus its pointers. A singly node holds one pointer. A doubly node holds two. A circular list adds no pointers per node. It reuses the next field that would otherwise be None. A sentinel adds one extra node for the whole list, not one per node.
You can measure the per-node difference:
import sys
s = SNode(1)
d = DNode(1)
print("singly node bytes:", sys.getsizeof(s))
print("doubly node bytes:", sys.getsizeof(d))The doubly node is larger by one slot, which is 8 bytes on a 64-bit build. Exact totals depend on your Python version. In CPython the node object and its header often cost more than the pointers, so the relative overhead is smaller than "double the pointers" suggests.
Choosing a variant
Exercise
A task queue only pushes to the back and pops from the front. A singly linked list keeps only a head reference. Which operation is slow, and what is the cheapest change that fixes it without adding a prev pointer?
Show answer
Push back is O(n), because it walks to the last node. Adding a tail reference makes push back O(1). Pop front stays O(1). Pop back would still be O(n), but this queue never uses it.