常规思路:使用一个变量作为中间变量
if(lists.length==0) return null
let res=lists[0]
for(var i=1;i<lists.length;i++){
res=mergeKList(res,lists[i])
}
return res
};
var mergeKList=function(l1,l2){
let res = new ListNode()
let l3=res
while (l1 && l2) {
if (l1.val < l2.val) {
l3.next=l1
l1 = l1.next
} else {
l3.next=l2
l2 = l2.next
}
l3=l3.next
}
l3.next=(l1?l1:l2);
return res.next
}
分治思想
let n=lists.length
if(n==0) return null
if(n==1) return lists[0]
if (n==2) return mergeKList(lists[0],lists[1])
let mid =parseInt(n/2)
let l1=[],
l2=[]
for(var i=0;i<mid;i++){
l1[i]=lists[i]
}
for(var i=0;i<n-mid;i++){
l2[i]=lists[i+mid]
}
return mergeKList(mergeKLists(l1),mergeKLists(l2))
};
var mergeKList=function(l1,l2){
let res = new ListNode()
let l3=res
while (l1 && l2) {
if (l1.val < l2.val) {
l3.next=l1
l1 = l1.next
} else {
l3.next=l2
l2 = l2.next
}
l3=l3.next
}
l3.next=(l1?l1:l2);
return res.next
}
var deleteDuplicates = function(head) {
if(!head) return null
let res=new ListNode()
let count=res
res.next=head
while(count.next&&count.next.next){
if(count.next.val==count.next.next.val){
let x=count.next.val
while(count.next&&count.next.val==x){
count.next=count.next.next
}
}else{
count=count.next
}
}
return res.next
}