Chapter 3

Linked lists

A list node contains data and links. Drawing node identities separately from pointer variables prevents most tracing mistakes. Changing a pointer variable does not change the node to which another variable points.

3.1 Link operations and boundary cases

Past papers · CS 2014–2026 (23 papers): 4 questions, 6 marks

In a singly linked list, head names the first node and the last node's next is null. In a circular list the last link returns to the first node. In a doubly linked list every node also has prev. A sentinel is a permanent non-data node used to simplify endpoint changes; do not count it as an element.

OperationSingly linked listDoubly linked list
Insert after known node ppSet new.next to p.next; then p.next to newAlso repair successor.prev and new.prev
Delete node after known ppSave victim; bypass it; release victim if ownedRepair both directions
Delete a given arbitrary nodeFind predecessor: Θ(n)\Theta(n) worst caseUnlink using prev and next: Θ(1)\Theta(1)
Delete headAdvance head; Θ(1)\Theta(1)Also clear new head.prev
Append with tail pointerΘ(1)\Theta(1)Θ(1)\Theta(1)
Delete tail with tail pointerΘ(n)\Theta(n) worst caseΘ(1)\Theta(1)

Here nn is current length; local update costs exclude allocation and reclamation. Copying successor data over a given non-tail singly linked node can simulate deletion in constant time, but cannot handle the tail and changes record identity. It is not a general constant-time deletion result.

3.2 Reversal, joining and sorted lists

Past papers · CS 2014–2026 (23 papers): 3 questions, 3 marks

For reversal, preserve the next unprocessed node before overwriting its link. At each iteration, prev heads the reversed prefix and cur heads the untouched suffix.

prev = null
cur = head
while cur != null:
    saved = cur.next
    cur.next = prev
    prev = cur
    cur = saved
head = prev

The algorithm performs one link reversal per node: Θ(n)\Theta(n) time, Θ(1)\Theta(1) auxiliary space. Recursive reversal uses Θ(n)\Theta(n) frames unless transformed.

A four-node list before and after in-place reversal.A four-node list before and after in-place reversal.

Joining disjoint unsorted lists is constant time when the first tail and both heads are available. Without that tail, locating it is linear. Empty lists need separate endpoint handling. Shared nodes can create a cycle; disjointness is a real precondition. Melding sorted lists by advancing the smaller head takes Θ(n1+n2)\Theta(n_1+n_2) worst-case time for lengths n1,n2n_1,n_2.

Changing a key in a sorted list may require relocating its node. Even with the node supplied, finding the new sorted position is Θ(n)\Theta(n) worst case. Sum such searches when comparing data structures for a workload.

3.3 Circular lists, cycles and traversal invariants

Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks

For a circular singly linked queue with only tail stored, head is tail.next; insert after tail and advance tail to enqueue, and bypass tail.next to dequeue. Deleting the sole node makes tail null. In a nonempty circular traversal, stop when you return to the starting node, not when the link becomes null.

For a null-terminated list, initialise a counter to zero and advance while the current node is non-null. A recursive size computation returns zero at null and otherwise returns one plus the suffix size. If the stated input is nonempty, a base case at node.next == null returns one instead.

Floyd's cycle test advances a slow pointer by one link and a fast pointer by two, checking null before each dereference. Meeting implies a cycle. After a meeting, reset one pointer to head; advance both by one until they meet at the entry. If the acyclic prefix length is μ\mu and cycle length λ\lambda, detection and entry finding take O(μ+λ)O(\mu+\lambda) time and constant auxiliary space. At detection, the travelled slow distance is a multiple of λ\lambda, which explains the reset rule.

Last-minute recall

  • Save the next link before overwriting it.
  • A tail pointer does not give the predecessor in a singly linked list.
  • Sentinels simplify links but are not data elements.
  • Maintain both prev and next in a doubly linked list.
  • Sorted insertion may spend linear time finding its position.
  • Check empty and one-node cases separately.
  • Shared suffixes make naive list concatenation unsafe.

Revise offline: the whole Programming and Data Structures handbook as one PDF

55 pages · 2.6 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections