您的位置:首页 > 其它

[LeetCode] Merge Two Sorted Lists

2015-08-18 17:03 239 查看
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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: