剑指offer-JZ36两个链表的第一个公共结点

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
热度指数:377198
本题知识点: 链表

题目描述

输入两个链表,找出它们的第一个公共结点。(注意因为传入数据是链表,所以错误测试数据的提示是用其他方式显示的,保证传入数据是正确的)

代码

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function FindFirstCommonNode(pHead1, pHead2)
{
    if(!pHead1 || !pHead2) {return null;}
    var arr = [];
    while(pHead1){
        arr.push(pHead1);
        pHead1 = pHead1.next;
    }
    while(pHead2){
        if(arr.indexOf(pHead2) >= 0) {return pHead2;}
        pHead2 = pHead2.next;
    }
    return null;
}

你可能感兴趣的:(刷题记录)