SeaSide-秦时明月

自我勉励 & 共同成长

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.

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


阅读更多
文章标签: java算法
个人分类: leetcode
上一篇链表中环的问题
下一篇Swap Nodes in Pairs
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭