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

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

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

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function FindFirstCommonNode(pHead1, pHead2)
{
    // write code here
    let current1 = pHead1;
    let current2 = pHead2;
//设置标记,第一个链表遍历,所有节点标志都设置为true
  while(pHead1!=null){
  pHead1.flag=true;
pHead1=pHead1.next;
  }
  //当第二个链表遍历的时候遇到标志为true的节点就说明是从这里相遇的
  while(pHead2!=null){
     if(pHead2.flag==true){
        return pHead2
    }else{
        pHead2=pHead2.next
    }
  }
   
  

   
}
module.exports = {
    FindFirstCommonNode : FindFirstCommonNode
};

全部评论

相关推荐

雪飒:我也遇见过,我反问他有考虑来华为od吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务