Heap pattern

Merge K Sorted Lists

Given an array of k linked lists, each sorted in ascending order, merge them into a single sorted linked list and return its head. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 23
  • Hard
  • 2 approaches

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

HeapLeetCode 23

Hard

From the first idea to the best one

  1. 01 Brute force · scan k heads each round O(N·k)O(1) space
  2. 02 Optimized · min-heap of heads O(N log k)O(k) space
Each approach plays step by step, with its time and space.

What you will learn

  • How to read Merge K Sorted Lists and spot the heap pattern in it
  • Brute force · scan k heads each round: O(N·k) time, O(1) space
  • Optimized · min-heap of heads: O(N log k) time, O(k) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Heap

See Merge K Sorted Lists run, step by step

Sign in and this page opens on the animation, with all 7 Heap problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.