Skip to content

Singly linked lists

Build a singly linked list from Node objects with data and next, and show that adding at the head takes O(1) while finding a value takes O(n).

It opens on

In this chain, can we jump from head straight to the 5 at the end, or must we pass through the 3 and the 8 first?

Why it matters

A linked list is the first structure we build from objects that refer to one another and not from a block of cells, and it trades what an array does well, reading by index, for what an array does badly, adding at the front.

Good to know first

  • Classes, objects and __init__: a class makes objects whose attributes are set in __init__
  • Aliasing and mutability: two names can refer to one object, and is tests that
  • Dynamic arrays and amortized cost: an array inserts at the front in Θ(n) because it moves every item

Step by step

  1. Nodes and the chain

    Build a chain of three Node objects with data and next, and read its values by following next from head.

    To build the chain we start from the end. Node(5) has next None; Node(8, c) is a node holding 8 whose next refers to the node c; Node(3, b) holds 3 and refers to b; and head = a makes head refer to the first node.

    the building code

    A common mix-up

    Next holds the next value, or a copy of the next node.

    In fact: Next holds a reference to the node object, so the nodes are shared and not copied.

  2. Traverse and find

    Write the loop that visits every node of a linked list and the find that returns the node holding a value or None, and state the cost of each.

    On the chain 3, 8, 5, cur is the node 3 and prints 3, moves to the node 8 and prints it, moves to the node 5 and prints it, and then cur is None, so the loop ends. The program prints 3, 8 and 5, one on each line.

    A common mix-up

    Find on a linked list can be as fast as binary search.

    In fact: Binary search needs to read the middle by index, and a linked list reaches position i only by i hops, so find is Θ(n) in the worst case.

  3. Add at the head

    Write prepend, which adds a value at the head in O(1), and compare the linked list and the array operation by operation.

    Take the chain 3, 8, 5 and call prepend(1). The expression Node(1, self.head) makes a new node holding 1 whose next refers to the current head, the node 3, and then self.head = ... moves head to the new node.

    A common mix-up

    A linked list is faster than an array at everything.

    In fact: Prepend is O(1) against Θ(n), but reading by position, which an array does in O(1), takes up to n hops in a linked list.

Try it

Build, in the same way, the chain 2, 7 with head referring to the node holding 2. Which Node calls do you write?Answer

c = Node(7), then head = Node(2, c)

Hint: Start from the last node

How many comparisons does find(4) make on the chain 5, 2, 8, and what does it return?Answer

3 comparisons, and it returns None

Hint: 4 is absent, so every node is compared

What chain results from prepending 7, then 3, then 6 to an empty list, and what is head.data?Answer

6, 3, 7 with head.data equal to 6

Hint: The last value prepended comes first

Is head.next is b True or False, and why?Answer

True, because next holds a reference to the node b and not a copy of it, so head.next and b are two names for one node.

What happens on pass 3, and what does find return?Answer

Cur.data is 5, which is not 9, so cur becomes None, the loop ends and find returns None, after 3 comparisons.

Singly linked lists

30 minutes every month. No card.