← DiffPush

Merge K Sorted Linked Lists

Standard Bar Heaps · Medium Problems O(n log k) · O(k)

The Radio Spectrum Channel Blender

A signal recorder captures k channels, each already timestamp-ordered, and must replay everything as one interleaved timeline. The blender keeps each channel's earliest unplayed event in a small pool, plays the overall earliest, and refills the pool from that channel's next event — an event at a time, never re-sorting.

Input: A list of k lists, each sorted in ascending order (lists may be empty).

Output: Return one sorted list containing every value from all lists.

Constraints

Examples

Example 1

Input: {"lists":[[1,4,5],[1,3,4],[2,6]]}
Output: [1,1,2,3,4,4,5,6]
The pool interleaves the three channels into one non-decreasing replay.

Example 2

Input: {"lists":[[],[]]}
Output: []
Empty channels contribute nothing to the pool.

Solve this in your browser →

Also on LeetCode ↗