首页 > 编程语言 > 详细

剑指offer(16)合并两个排序的链表

时间:2018-09-05 23:03:12      阅读:186      评论:0      收藏:0      [点我收藏+]

题目描述:

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

解题代码:

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function Merge(pHead1, pHead2)
{
    // write code here
    if(pHead1 == null && pHead2 == null){
        return null;
    }
    if(pHead1 == null){
        return pHead2;
    }
    if(pHead2 == null){
        return pHead1;
    }
    var newHead = null;
    if(pHead1.val <= pHead2.val){
        newHead = pHead1;
        newHead.next = Merge(pHead1.next,pHead2);
    }
    else{
        newHead = pHead2;
        newHead.next = Merge(pHead1,pHead2.next);
    }
    return newHead;
}

 

剑指offer(16)合并两个排序的链表

原文:https://www.cnblogs.com/3yleaves/p/9594938.html

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