Singly Linked List

Deterministic node pointer links and sequential address transitions

HEAD
12idx: 0
node-0
45idx: 1
node-1
8idx: 2
node-2
TAIL
27idx: 3
node-3
NULL
null
Step 1 of 1Type: idle

Initialized linked list with 4 nodes

Speed: 1x

Overview & Concept

A Singly Linked List is a linear data structure consisting of discrete nodes connected by unidirectional pointers. Each node contains a data payload and a "next" pointer referencing the succeeding node in memory. Because nodes are dynamically allocated in heap memory rather than residing in contiguous physical RAM, insertion and deletion at the head execute in constant O(1) time without element shifting.

How It Works

Non-Contiguous Dynamic Memory: Nodes are scattered throughout heap memory, connected solely by pointer references.

Head Pointer: The anchor pointer identifying the start of the chain. An empty list has head = null.

O(1) Prepend: Creating a node and pointing its next pointer to the current head updates the list in O(1) time.

O(n) Traversal: Accessing an arbitrary index requires sequential pointer chasing from head to node k.

Step-by-Step Execution

1. Node AllocationAllocate memory for a new node on the heap and store the payload value.
2. Pointer SplicingSet newNode.next = targetNode.next to link to the subsequent chain.
3. Previous Link UpdateSet previousNode.next = newNode to securely insert the node into the chain.
4. Head / Tail MaintenanceUpdate head or tail pointers if the mutation occurs at list boundaries.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(1) prependO(n) index accessO(n) searchO(n)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
// Singly Linked List Operations
operation prepend(value):
  newNode = new Node(value)
  newNode.next = head
  head = newNode

operation deleteAfter(node):
  if node.next != null:
    toDelete = node.next
    node.next = toDelete.next
    free(toDelete)
Algorithmic Invariants
  • Acyclic Invariant: In a standard singly linked list, starting at head and following next pointers terminates at null.
  • Reachability Invariant: Every valid node in the list is reachable from head in finite steps.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Dynamic size: grows and shrinks seamlessly without expensive full-array reallocations.
  • •Instant O(1) insertions and deletions at the head or known pointer locations.
  • •Zero wasted memory on pre-allocated empty capacity.

Trade-offs & Limitations

  • •No random access: accessing index k requires O(k) linear pointer traversal.
  • •Memory overhead: extra 4-8 bytes per node for storing pointer references.
  • •Poor CPU cache locality due to scattered non-contiguous memory allocations.

Real-World Applications

Separate Chaining in Hash Tables

Collision resolution chains storing colliding keys at a common bucket index.

Memory Allocation Free Lists

Operating system heap allocators tracking available memory blocks.

Undo Stacks in Embedded Devices

Dynamic history tracking where fixed array buffers cannot be pre-allocated.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When frequent insertions and deletions occur at the beginning of the sequence.
  • ✓When the maximum number of items is unknown and dynamic growth is essential.

Consider Alternatives When

  • ✕When random access by index is frequent (use Arrays).
  • ✕When memory per element must be minimized and cache locality is vital.

Compare trade-offs and complexity across complementary tools in AlgoViz.