合并两个排序的链表

题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
思想:比较两个链表头节点值确定新链表头节点,之后节点仍是找到两个链表头节点pHead1.next和pHead2,即递归问题
解决鲁棒性,对空链表单独处理,合并结果就是另一个链表
 
function Merge(pHead1, pHead2)
{
    if(pHead1==null){
        return pHead2;
    }else if(pHead2==null){
        return pHead1;
    }
    var current=null;
    if(pHead1.val<pHead2.val){
       current=pHead1;
        pHead1.next=Merge(pHead2,current.next);
    }else{
        current=pHead2;
        pHead2.next=Merge(pHead1,current.next);
    }
    return current;
    // write code here
}
posted @ 2017-06-06 14:49  我叫王自信  阅读(97)  评论(0编辑  收藏  举报