题解 | #合并k个已排序的链表#
合并k个已排序的链表
http://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6
function ListNode(x){
this.val = x;
this.next = null;
}
function mergeKLists( lists ) {
// write code here
var arr=[]
for(var i=0;i<lists.length;i++){
var cur=lists[i]
while(cur){
arr.push(cur.val);
cur=cur.next;
}
}
arr.sort((a,b)=>a-b);
var dummy=new ListNode(-1)
var temp=dummy;
for(var i=0;i<arr.length;i++){
temp.next=new ListNode(arr[i])
temp=temp.next;
}
return dummy.next;
}
this.val = x;
this.next = null;
}
function mergeKLists( lists ) {
// write code here
var arr=[]
for(var i=0;i<lists.length;i++){
var cur=lists[i]
while(cur){
arr.push(cur.val);
cur=cur.next;
}
}
arr.sort((a,b)=>a-b);
var dummy=new ListNode(-1)
var temp=dummy;
for(var i=0;i<arr.length;i++){
temp.next=new ListNode(arr[i])
temp=temp.next;
}
return dummy.next;
}