标签:
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
这道题还是很有意思的><个人觉得题读懂后就没啥难的了。
当然这里其实也还可以另外新弄一个listnode来辅助,这种思路更为常见,不过简化后就发现不需要新建啦~
代码如下。~
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null&&l2==null){
return null;
}
if(l1==null&&l2!=null){
return l2;
}
if(l2==null&&l1!=null){
return l1;
}
if(l1.val>l2.val){
l2.next=mergeTwoLists(l1,l2.next);
return l2;
}else{
l1.next=mergeTwoLists(l2,l1.next);
return l1;
}
}
}
[LeetCode] Merge Two Sorted Lists
标签:
原文地址:http://www.cnblogs.com/orangeme404/p/4739928.html