IB Computer Science HL · Year 2 · Lesson 23

Doubly and circular linked lists

Construct and trace two required linked-list variants, paying attention to bidirectional references and circular stopping conditions.

Paper 250 minutesB4.1.2 · B4.1.3
Targets

More links create more possibilities — and more failure points

2 / 10
  • construct and trace doubly linked nodes with next and prev;
  • construct and trace circular links;
  • apply insertion, deletion, traversal and search to these variants;
  • evaluate the extra memory/complexity against their behavioural advantages.
B4.1.2B4.1.3
Learn + retrieve

Rapid Recall Deck

3 / 10

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

Doubly linked

Every connection has a reverse direction too

4 / 10
class DNode:
    def __init__(self, value):
        self.value = value
        self.prev = None
        self.next = None

Construct A ⇄ B ⇄ C

Create nodes A, B and C and connect all required forward and backward references.

a = DNode("A")
b = DNode("B")
c = DNode("C")

a.next = b
b.prev = a
b.next = c
c.prev = b
Insertion + deletion

Maintain both directions

5 / 10

Insert X between A and B

Assume a.next is b and b.prev is a. Construct the four reference updates needed to insert node x between them.

x.prev = a
x.next = b
a.next = x
b.prev = x

Delete the middle node

For A ⇄ B ⇄ C, B is being removed and you already have a reference to B. Construct the two neighbour updates that reconnect A and C.

b.prev.next = b.next
b.next.prev = b.prev
Circular linked

The stopping condition is part of the algorithm

6 / 10
a = Node("A")
b = Node("B")
c = Node("C")
a.next = b
b.next = c
c.next = a
head = a

Trace one cycle

Starting from head, list the values encountered during exactly one complete traversal.

A, B, C. After C, next returns to A, which signals completion of one cycle.

Construct safe traversal

Assume the circular list is non-empty. Construct a loop that prints each node exactly once.

current = head
while True:
    print(current.value)
    current = current.next
    if current is head:
        break
Circular search

Never wait for None in a true cycle

7 / 10

Construct circular search

Assume head is either None or the first node of a singly circular linked list. Construct a function contains(head, target) that checks every node at most once.

def contains(head, target):
    if head is None:
        return False

    current = head
    while True:
        if current.value == target:
            return True
        current = current.next
        if current is head:
            return False
B4.1.2 · Evaluate

Choose the variant from the behaviour

8 / 10

Evaluate the variants

A browser history must move both backward and forward. A round-robin scheduler repeatedly cycles through active processes. For each application, choose singly, doubly or circular linked list and justify.

Required movement
End behaviour
Reference memory / update complexity
Judgement

Browser history: doubly linked is a natural fit because the structure must move in both directions; the extra previous reference is justified. Round-robin scheduler: circular linked is natural because after the final process the traversal returns to the first; correct cycle termination/removal logic is essential. A singly non-circular list would be simpler but would not model either requirement as directly.

Exit check

Structure determines the termination condition

9 / 10

Exit bug

A circular traversal uses while current is not None:. Explain why this can fail and state a correct stopping idea.

In a valid circular list, current never becomes None, so the loop can run forever. Stop after the reference returns to the starting node/head, or use another explicit count/sentinel that guarantees one cycle.

Homework

Finish the learning cycle

10 / 10

IA — main task

Continue Criterion D implementation/testing and record one bug or edge case you tested deliberately.

Paper 2 — short practice

Reconstruct doubly linked insertion/deletion and circular traversal/search from memory, then explain one memory/performance trade-off for each variant.