题解 | 两个链表的第一个公共结点
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
/*
{1,2,3},{4,5},{6,7}
{6,7}
{1},{2,3},{}
{}
}*/
function FindFirstCommonNode(pHead1, pHead2) {
// write code here
while (pHead1 || pHead2) {
if (pHead1) {
if (pHead1.flag) {
return pHead1;
}
pHead1.flag = 1;
pHead1 = pHead1.next;
}
if (pHead2) {
if (pHead2.flag) {
return pHead2;
}
pHead2.flag = 1;
pHead2 = pHead2.next;
}
}
return null;
}
module.exports = {
FindFirstCommonNode: FindFirstCommonNode,
};
查看1道真题和解析