题解 | #合并两个排序的链表#

合并两个排序的链表

http://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337

还可以用递归是真的牛逼

function ListNode(x){
    this.val = x;
    this.next = null;
}
function Merge(pHead1, pHead2)
{
    // write code here
   
        let p=new ListNode(-1)
        let res=p 
        while(pHead1!==null&&pHead2!==null){
            if(pHead1.val>=pHead2.val){
               p.next=pHead2
               pHead2=pHead2.next
            }else{
               p.next= pHead1
               pHead1=pHead1.next
            }
             p=p.next
        }
        
         if(pHead1===null){
            p.next=pHead2
        }else if(pHead2===null){
           p.next=pHead1}
       return res.next
}
module.exports = {
    Merge : Merge
};
全部评论

相关推荐

这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
你找工作的时候用AI吗?
点赞 评论 收藏
分享
醉蟀:你不干有的是人干
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 14:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务