VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 09: Heaps and top-k

Lesson 9.7 of 12 in this module Unrolled Pro

Merge K Sorted Lists

Merge k sorted linked lists (N nodes in total) into one sorted list. From collecting every value and sorting (O(N log N)), to merging the lists one after another (O(N · k), early nodes re-walked k times), to merging in pairs like merge sort and to a min-heap of the k current fronts, both O(N log k). Then sorting data bigger than memory by merging sorted runs from disk, very large k, and merging per-shard retrieval results by score while reading only the top K.

about 10 min 4 Python examples 2 quick checks

Problem. You're given k linked lists, each sorted ascending. Merge them into one sorted list and return its head. Let N be the total number of nodes.

text
[1 → 4 → 5,  1 → 3 → 4,  2 → 6]   →   1 → 1 → 2 → 3 → 4 → 4 → 5 → 6
[]                                 →   None

Lesson 7.5 merged two lists in O(m + n), and its Change A sketched k lists with a heap. Here the k-way merge is the main event: four approaches, three different costs, and the reasons a heap is the default for streams while pairwise merging wins in other settings.

ML context. k-way merges are everywhere data is sharded: per-worker logs sorted by timestamp, per-shard search results sorted by score, sorted runs written to disk by a large sort, per-partition outputs of a map-reduce job. The heap merge reads each input sequentially and holds only k items in memory, which is exactly what you need when the inputs are files or network streams (Changes A and C).


1. Step 1: Clarify

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

Unlock "Merge K Sorted Lists"

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