A Linked List as Linked Nodes

A singly linked list represents order through references from one node to the next. Reading an offset generally requires following that many links.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace the links
  2. Count finding and editing separately
  3. Save the link that would be lost by overwriting first
  4. Distinguish finding a node from changing known links

Trace the links

A head pointing to A, A pointing to B, and B pointing to C describes a three-item order. Inserting X after A changes A to point to X and X to point to B. Reference binding explains why edits to existing links reach traversals through the same nodes.

Count finding and editing separately

Once the preceding node is known, changing those links takes a bounded number of operations. Finding that node can require a scan. Compare a sequence index on a random-access representation.[1]

Save the link that would be lost by overwriting first

In the list A to C to D, insert B between A and C by setting B's next link to C, then A's next link to B. If A loses its link before C is saved, the suffix can become unreachable from the head. See tracing program state for drawing individual reference changes.

Initially A.next = C, C.next = D
Set B.next = A.next
Set A.next = B
Traversal from A now visits A, B, C, D

Distinguish finding a node from changing known links

Given a handle to A, insertion after it changes a fixed number of links. Finding A by an arbitrary key may still require scanning the list. Test inserting after the tail and before the first node through the head reference.

References

  1. Jeff Erickson, Algorithms ↩