[javascript]代码库
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function(lists){
let head=null,len=0;
for(let a=0;a<lists.length;a++){
if(!lists[a]){
lists.splice(a,1);
a--;continue;
}
if(a+1<lists.length&&!lists[a+1]){
lists.splice(a+1,1);
a--;continue;
}
if(!head)head=lists[a];
let h1=lists[a],h2=lists[a+1];
if(h2==undefined) h2=null;
len++;
while(h1.next){
h1=h1.next;
len++;
}
h1.next=h2;
}
// console.log(len,head);
let h1=head;
for(let a=0,b=1;a<len-b;a++){
let h2=h1.next;
//console.log(h1.val,h2.val)
if(h1.val>h2.val){
let tmp=h1.val;
h1.val=h2.val;
h2.val=tmp;
}
h1=h1.next;
if(a==len-1-b){
h1=head;a=-1;b++;
}
}
// console.log(head)
return head;
};