CMD Guide
HomeDSAFoundations

Linked List

Linked List

Imagine a treasure hunt. Each clue tells you the location of the next clue, but nothing tells you where clue #5 is directly — you must start at clue #1 and follow the chain. A linked list is exactly this: a sequence of independent boxes (called nodes) scattered anywhere in memory, where each box holds a value plus the address of the next box. Contrast this with an array, which is one solid block of numbered lockers you can jump into instantly. The linked list trades that instant jump for a superpower array lacks: you can splice a new box into the middle of the chain by rewriting just one or two pointers, without shifting anything else.

Precise definition

A linked list is a linear data structure made of nodes. Each node stores: (1) a value (the payload), and (2) one or more references (pointers) to other nodes. The list is accessed through a head pointer to the first node; the last node's next pointer is null, marking the end.

Crucially, nodes are not contiguous in memory. There is no index arithmetic — to reach position k you must walk k links from the head.

Worked example: search vs. insert, with the operations counted

Take the list head → 10 → 20 → 30 → null from the diagram. Let's count real work.

Search for 30. Start at head (10). Compare 10 ≠ 30, follow next. At 20: 20 ≠ 30, follow next. At 30: match. That's 3 node visits and 3 comparisons to find the element at position 3. To confirm a value is absent, you must walk all the way to null — the full n visits. Searching is O(n), no shortcut, because there is no random access.

Insert 25 after the node holding 20. Assume we already hold a reference to node(20). Steps: (1) allocate a new node with value 25; (2) set new.next = node20.next (which is node(30)); (3) set node20.next = new. That is 2 pointer writes and 1 allocation — a fixed amount of work regardless of list size. This is the headline win: O(1) insertion at a known position. Compare an array of 1,000,000 elements: inserting in the middle forces you to shift ~500,000 elements right — O(n) copying.

The catch: that O(1) assumed you already had the node. If you only know the value 20, you first pay O(n) to find it. The cheap splice and the expensive search are separate costs — interviewers love to see if you conflate them.

Complexity summary (be honest about the fine print)

Common pitfalls and what an interviewer probes

When it matters in practice, and the trade-offs

Linked lists shine when you do many insertions and deletions at ends or at positions you already hold, and rarely need random access: implementing stacks and queues, the collision chains in a hash table, the eviction order in an LRU cache (doubly linked list + hash map), and adjacency lists in graphs. The OS uses them for process and free-memory lists.

But be skeptical — in modern practice arrays (dynamic arrays / ArrayList / slice) usually win even for insert-heavy workloads. Two reasons: (1) memory overhead — every node pays for a pointer (8 bytes) or two, plus allocator bookkeeping, so a linked list can use 2–3× the memory of a packed array. (2) cache locality — arrays sit contiguously and stream through the CPU cache; linked-list nodes are scattered, so each hop is a likely cache miss. A cache miss costs ~100× a cache hit, so an O(n) array scan often beats an O(n) linked scan by a large constant factor. The correct interview answer is nuanced: choose a linked list for guaranteed O(1) splicing at known nodes and pointer stability; choose an array when you need indexing, iteration speed, or compactness.

Key takeaways

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

Stuck on Linked List? 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 **Linked List** (DSA) and want to truly understand it. Explain Linked List 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 **Linked List** 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 **Linked List** 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 **Linked List** 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