CMD Guide
HomeDSALinked List

Types of LinkedList

A linked list's behavior — which directions you can walk it, and whether it ever ends — is entirely determined by two design choices baked into the node: how many pointers it carries (one next, or next plus prev), and what the last node's pointer holds (null, or a reference back into the list). Three combinations of those choices give the singly, doubly, and circular variants; picking the wrong one is what turns an otherwise-correct algorithm from O(1) into O(n), or makes it loop forever.

Recognize the pattern

Structure and cost, derived

TypePointers/nodeExtra spaceDelete a given node X (handle already known)Backward walk
Singly (SLL)1 (next)0O(n) — must rescan from head to find X's predecessorimpossible
Doubly (DLL)2 (next, prev)O(1) extra per node / O(n) totalO(1): X.prev.next=X.next; X.next.prev=X.prevO(n) via prev chain
Circular singly1, tail→head0O(n), same scan issue as SLLimpossible
Circular doubly2, wrapped both endsO(1) extra per node / O(n) totalO(1), same as DLLO(n)

The DLL's O(1) delete isn't magic: it trades one extra pointer per node (O(n) total across the list) for eliminating the O(n) predecessor scan an SLL needs before every mid-list delete. That single trade is the whole story. Traversal for all variants is O(n) since each node visit is O(1) and there are n nodes — no shortcuts, no random access (unlike an array's O(1) index).

Brute force vs. optimal: deleting a node by value

Brute force (any type): scan from head comparing values, track previous node, relink — O(n) time, O(1) space. The scan-and-relink logic is identical on SLL, DLL, and CLL, but the loop's termination check is not: on SLL/DLL you stop at node == null; on a circular list there is no null, so that same check never fires and the scan spins forever unless you instead stop at node == head (or count visited nodes) — see Pitfalls.

Where the type actually changes the answer: if you are handed a direct reference to the node to delete (not a value to search for), an SLL forces you back to O(n) to find its predecessor, while a DLL does it in O(1) because node.prev is already stored. This is why DLLs back LRU caches: the hash map gives you the node pointer directly, and the DLL lets you unlink it in O(1).

Traced example: deleting node 'B' by reference, DLL vs SLL

List: A(10) ↔ B(20) ↔ C(30), and we already hold a reference to node B (e.g. from a hash map lookup).

StepSLL (must find predecessor)DLL (O(1))
1cur = head = A; check A.next == B → true, so prev = Aprev = B.prev → A (already known)
2A.next = B.next (= C)A.next = B.next (= C)
3free BC.prev = A; free B
CostO(k) to find predecessor (k = B's position) + O(1) relinkO(1) total

Result both cases: A(10) ↔ C(30).

// Minimal DLL delete-by-reference (Java)
class Node {
    int val;
    Node prev, next;
    Node(int val) { this.val = val; }
}

class DoublyLinkedList {
    Node head, tail;

    void deleteNode(Node x) {
        if (x.prev != null) x.prev.next = x.next;
        else head = x.next;               // x was head
        if (x.next != null) x.next.prev = x.prev;
        else tail = x.prev;                // x was tail
        x.prev = null; x.next = null;      // help GC, avoid stale links
    }
}

Pitfalls

When to use / when not

SLL — use for stacks, forward-only iteration, minimal-memory adjacency lists. Avoid when you need O(1) deletion by reference or backward traversal.

DLL — use for LRU caches, deques, undo/redo, text buffers. Costs one extra reference per node (4 bytes on a 64-bit JVM with compressed oops, 8 without — and 8-byte object alignment can round that up or absorb it entirely, e.g. a 12-byte-header + int + next node pads 20 → 24 bytes, the same size a prev field needs) and more bookkeeping on every mutation; skip it if you never delete by direct node reference and memory is tight.

CLL — use for round-robin schedulers, circular buffers, repeating structures. Avoid for generic list-of-items code — it invites infinite-loop bugs for no benefit.

Named alternative — dynamic array (ArrayList): gives O(1) random access and better cache locality than any linked list (contiguous memory), but insert/delete in the middle is O(n) due to shifting, versus O(1) relink once you're at the position in a linked list. Choose array-backed structures when access pattern is index-heavy; choose linked lists when you mutate frequently at known positions/ends and don't need random access.

Takeaways

Recall: Given only a reference to a node in the middle of the list (no head pointer, no search), which list type lets you delete it in O(1), and why does an SLL fail at this?


Synthesized from standard DSA references (GeeksforGeeks, CLRS linked list chapter) and verified traces; adapted for this guide.

🤖 Don't fully get this? Learn it with Claude

Stuck on Types of LinkedList? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Types of LinkedList** (DSA) and want to truly understand it. Explain Types of LinkedList from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Types of LinkedList** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Types of LinkedList** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Types of LinkedList** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes