首页 > 解决方案 > 以最有效的方式将几个预先排序的列表合并到一个联合列表中(并获取其中的顶部元素)

问题描述

我有这样一个预设:

标签: algorithmlistlinked-list

解决方案


使用任何基于优先级队列的数据结构:

priority queue q = empty
for each list add first element to q
create an array next that contains next elements for every list (initially next element is a second element)

while result list is not full
    take top element from the q and add to the result list
    add next element of the corresponding list to the q (if any)
    update next element of the corresponding list

推荐阅读