首页 > 其他 > 详细

[LeetCode] Merge Two Sorted Lists

时间:2015-08-18 19:07:52      阅读:202      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!