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.
| Operation | Singly linked list | Doubly linked list |
|---|---|---|
| Insert after known node | Set new.next to p.next; then p.next to new | Also repair successor.prev and new.prev |
| Delete node after known | Save victim; bypass it; release victim if owned | Repair both directions |
| Delete a given arbitrary node | Find predecessor: worst case | Unlink using prev and next: |
| Delete head | Advance head; | Also clear new head.prev |
| Append with tail pointer | ||
| Delete tail with tail pointer | worst case |
Here 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: time, auxiliary space. Recursive reversal uses frames unless transformed.
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 worst-case time for lengths .
Changing a key in a sorted list may require relocating its node. Even with the node supplied, finding the new sorted position is 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 and cycle length , detection and entry finding take time and constant auxiliary space. At detection, the travelled slow distance is a multiple of , 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.