题解 | #两个链表的第一个公共结点#

两个链表的第一个公共结点

https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function FindFirstCommonNode(pHead1, pHead2) {
    let stack1 = [],
        stack2 = [];
    while (pHead1) {
        stack1.push(pHead1);
        pHead1 = pHead1.next;
    }
    while (pHead2) {
        stack2.push(pHead2);
        pHead2 = pHead2.next;
    }
    let len=Math.min(stack1.length,stack2.length)
    let result=null
    for(let i=1;i<=len;i++){
        if(stack1.at(-i)===stack2.at(-i)){
            result=stack1.at(-i)
        }else{
            break
        }
    }
return result
    // write code here
}
module.exports = {
    FindFirstCommonNode: FindFirstCommonNode,
};

全部评论

相关推荐

菠落蜜:这个是系统自动投的,不是hr主动打招呼。更抽象的还有ai回复
我的秋招日记
点赞 评论 收藏
分享
牛油果甜奶昔:别的先不说,牛客还能内推护士?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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