Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Merge K Sorted Lists

LeetCode #23Hard
Min-heap of the k current heads

Given an array of k linked lists, each sorted in ascending order, merge them into a single sorted linked list and return its head.

Asked atAmazonGoogleMetaMicrosoft
step 1 / 79
list 0
list 1
list 2
1
4
7
2
5
3
6
8
[0][1][2][3][4][5][6][7]
merged output
Brute force · scan k heads each round
1given k sorted lists
2repeat until all empty:
3 scan the ≤ k current heads
4 move the minimum to output
5 advance that list
state
  • k lists3
  • total8

line 1Merge 3 sorted lists into one sorted list. The answer always starts with the smallest CURRENT head across the lists. Brute: each round, scan all 3 heads and take the min.