您的位置:首页 > 编程语言 > Java开发

java递归

2015-10-21 20:39 441 查看
public static ListNode mergeKLists(ListNode[] lists){
return partion(lists,0,lists.length-1);
}
//将k个lists分解成两两合并
public static ListNode partion(ListNode[] lists,int s,int e){
if(s==e)  return lists[s];
if(s<e){
int q=(s+e)/2;
ListNode l1=partion(lists,s,q);
ListNode l2=partion(lists,q+1,e);
return merge(l1,l2);
}else
return null;
}

//This function is from Merge Two Sorted Lists.
public static ListNode merge(ListNode l1,ListNode l2){
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=merge(l1.next,l2);//合并的递归
return l1;
}else{
l2.next=merge(l1,l2.next);
return l2;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  递归