Attic
DSALinked Lists

Overview

Dynamic linear memory chains, singly vs doubly vs circular representations, node layouts, and array trade-offs

A Linked List is a fundamental linear data structure in which elements (called nodes) are stored non-contiguously in heap memory. Instead of relying on physical memory offsets like an array, each node encapsulates a data payload and one or more reference pointers pointing to neighboring nodes.


Memory Architecture: Array vs Linked List

Architectural Trade-offs

CriterionArray (int[] / ArrayList)Linked List (ListNode)
Physical MemoryContiguous, pre-allocated memory blockDispersed nodes on JVM heap
Element Access (get(i))O(1) direct memory address calculationO(n) sequential pointer traversal
Head Insertion / DeletionO(n) (must shift entire buffer right/left)O(1) (rewire single reference pointer)
Tail AppendO(1) amortizedO(1) (with tail reference) or O(n)
Memory OverheadMinimal (pure data values)High (additional 8-byte reference pointer per node)
Cache LocalityHigh spatial locality (prefetches into L1/L2 CPU cache)Poor cache locality (pointer chasing causes cache misses)
ResizingRequires new buffer allocation + copy (O(n))Naturally dynamic (allocates nodes on demand)

Primary Linked List Variants

1. Singly Linked List

Each node points strictly to the next node. Traversal is strictly unidirectional:

[ Head: 10 ] ---> [ 20 ] ---> [ 30 ] ---> null
ListNode.java
public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {}
    public ListNode(int val) { this.val = val; }
    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

2. Doubly Linked List

Each node contains two pointers: prev and next. Allows bidirectional traversal and O(1) node removal when a direct node reference is held:

null <--- [ Head: 10 ] <===> [ 20 ] <===> [ Tail: 30 ] ---> null
DoublyListNode.java
public class DoublyListNode {
    public int val;
    public DoublyListNode prev;
    public DoublyListNode next;

    public DoublyListNode(int val) {
        this.val = val;
    }
}

3. Circular Linked List

The final node's next pointer references the head node rather than null. Common in round-robin task schedulers and audio playback loops:

[ Head: 10 ] ---> [ 20 ] ---> [ Tail: 30 ]
     ^                              |
     |------------------------------|

Detailed Guides

On this page