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
Asymptotic Complexity Analysis
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Complexity Bound | O(1) prepend | O(n) index access | O(n) search | O(n) |
| Explanation | Optimal 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
// 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)- 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.
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.
Explore Related Algorithms
Compare trade-offs and complexity across complementary tools in AlgoViz.