每两个一组合并有序链表,将结果覆盖原数组,同时更改数组长度,直至数组长度为1
function merge(h1,h2){
let empty = {};
let pre = empty;
while(h1!=null && h2!=null){
if(h1.val<h2.val){
pre.next = h1;
h1 = h1.next;
}else{
pre.next = h2;
h2 = h2.next;
}
pre = pre.next;
}
pre.next = h1!=null ? h1 : h2;
return empty.next;
}
function mergeKLists(lists) {
while(lists.length > 1){
let index = 0;
for(let i=0; i<lists.length; i+=2){
lists[index++] = merge(lists[i],lists[i+1] || null);
}
lists.length = index;
}
return lists[0];
}