查看: 95|回复: 0

用js刷剑指offer(合并两个排序的链表)

[复制链接]

0

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2012-1-12
发表于 2019-10-8 16:25:00 | 显示全部楼层 |阅读模式

题目描述

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

牛客网链接

js代码

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function Merge(pHead1, pHead2)
{
    // write code here
    if (!pHead1) return pHead2
    if (!pHead2) return pHead1
    let now = new ListNode(0)
    const root = now
    while (pHead1 && pHead2){
        if (pHead1.val > pHead2.val){
            now.next = pHead2
            now = now.next
            pHead2 = pHead2.next
        }else{
            now.next = pHead1
            now = now.next
            pHead1 = pHead1.next
        }
    }
    if (pHead1) {
        now.next = pHead1
    }else{
        now.next = pHead2
    }
    return root.next
}


来源:https://www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffer-he-bing-liang-ge-pai-xu-.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部