首页 > 学院 > 开发设计 > 正文

[LeetCode] Merge Two Sorted Lists

2019-11-15 01:11:03
字体:
来源:转载
供稿:网友
[LeetCode] Merge Two Sorted Lists

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;        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表