递归版实现
来源:10-5 LeetCode:23. 合并K个排序链表

慕用6828665
2021-01-26
let start2 = {value: 0, next: undefined};
let p2 = start2;
// 递归函数
// 主要思路每次把pop后的heap数组与pop出的那个next对象重新组装成新数组
var fn2 = (arr) => {
// 当数组为空,证明遍历结束
if(!arr.length) return false
// 这里每次要生成新的实例,否则会出现重复添加
const h2 = new MinHeap();
arr.forEach(obj => {
h2.insert(obj)
})
// 这部分跟视频逻辑一样
const head = h2.pop();
p2.next = head;
p2 = p2.next;
// 这里主要实现数组合并作为递归函数的参数
let concat = […h2.heap]
if(head.next){
concat.push(head.next)
}
fn2(concat)
}
fn2(arr)
写回答
1回答
-
lewis
2021-01-27
good
00
相似问题