合并两个排序的链表 题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,要求合成后的链表满足单调不减规则。
解:
public class Solution {
public ListNode
Merge(ListNode list1
,ListNode list2
) {
if(list1
==null
){return list2
};
if(list2
==null
){return list1
};
ListNode newNode
= null
;
if(list1
.val
<list2
.val
){
newNode
= list1
;
list1
= list1
.next
;
newNode
.next
= Merge(list1
,list2
);
}else{
newNode
= list2
;
list2
= list2
.next
;
newNode
.next
=Merge(list1
,list2
);
}
return newNode
;
}
转载请注明原文地址:https://blackberry.8miu.com/read-43058.html