VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 07: Linked lists and caches

Lesson 7.1 of 8 in this module Unrolled Pro

Nodes, Pointers and the Dummy-Head Trick

A linked list trades O(1) indexing for O(1) insertion and deletion anywhere you already hold a pointer. From Python lists, where inserting at the front shifts everything (O(n)), to nodes and next pointers, to the dummy-head trick that removes every special case for the first node. Then what costs what, why Python's deque is built from linked blocks, and the ML case: a free list that hands out KV-cache blocks in O(1), stored as an array of next indices.

about 10 min 5 Python examples 2 quick checks

The question this module answers: what if you need to insert and remove in the middle, often, without moving everything else? An array keeps items side by side, so position i is one multiplication away, but inserting at the front shifts every item. A linked list keeps each item in its own node with a pointer to the next one: inserting or removing next to a node you hold is O(1), and finding position i is O(i).

ML context. You rarely write a linked list in Python application code, but linked structures are everywhere underneath:

  • Caches: an LRU cache (lesson 7.7) is a hash map plus a doubly linked list, so moving an entry to "most recent" and evicting the oldest are both O(1). Embedding caches and prompt caches work this way.
  • Memory allocators: serving engines split the KV cache into fixed-size blocks and keep the free ones in a free list; allocating and freeing a block is O(1) (Section 5).
  • Queues: collections.deque is a doubly linked list of fixed-size blocks.

1. The problem: inserting at the front

Task: keep a sequence where new items are often added at the front, like the most recent requests or a history you prepend to.

A Python list is a contiguous array of pointers. Inserting at position 0 moves all n of them: O(n) per insert, O(n²) for n inserts.

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Nodes, Pointers and the Dummy-Head Trick"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee