关闭

Merge Two Sorted Lists

标签: java算法
93人阅读 评论(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.

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }

        ListNode mergeHead;
        if(l1.val < l2.val){
            mergeHead = l1;
            mergeHead.next = mergeTwoLists(l1.next, l2);
        }
        else{
            mergeHead = l2;
            mergeHead.next = mergeTwoLists(l1, l2.next);
        }
        return mergeHead;
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46705次
    • 积分:2117
    • 等级:
    • 排名:第18013名
    • 原创:162篇
    • 转载:40篇
    • 译文:2篇
    • 评论:3条
    最新评论