Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Merge k Sorted Lists
Bookmark
Input
Example 1
Example 2
Example 3
Custom
lists
=
[[1,4,5],[1,3,4],[2,6]]
input lists
min-heap · size
0
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
out
the heap always holds one front node per list; pop the smallest, repeat
input lists
min-heap · size
0
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
out
the heap always holds one front node per list; pop the smallest, repeat
input lists
min-heap · size
2
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
L0
1
L1
push head of
L1
=
1
input lists
min-heap · size
2
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
1
L1
2
L2
append
1
to the merged list ·
1
nodes
input lists
min-heap · size
2
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
1
2
L2
4
L0
append
1
to the merged list ·
2
nodes
input lists
min-heap · size
2
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
1
2
4
L0
3
L1
append
2
to the merged list ·
3
nodes
input lists
min-heap · size
3
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
1
2
4
L0
3
6
L2
4
L1
L1
advances → push
4
input lists
min-heap · size
3
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
1
2
4
3
6
L2
4
L1
5
L0
L0
advances → push
5
input lists
min-heap · size
2
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
1
2
4
3
6
L2
4
5
L0
list
L1
is exhausted, nothing to push
input lists
min-heap · size
0
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
min
out
dummy
1
1
2
4
3
6
L2
4
5
pop min =
6
(from
L2
)
input lists
min-heap · size
0
L0
∅
1
4
5
L1
∅
1
3
4
L2
∅
2
6
out
dummy
1
1
2
4
3
6
4
5
merged = [1, 1, 2, 3, 4, 4, 5, 6]
algo
master
.
io
Step:
Merge k sorted lists by always taking the smallest available head
0 / 30
Input
Example 1
Example 2
Example 3
Custom
lists
=
[[1,4,5],[1,3,4],[2,6]]
0 / 30
algo
master
.
io
Step:
Merge k sorted lists by always taking the smallest available head