非常好理解的解法
先遍历链表1,将flag设为1;再遍历链表2,将flag设为0,在找出一个flag为0的节点就是公共节点
function ListNode(x){
this.val = x;
this.next = null;
}
function FindFirstCommonNode(pHead1, pHead2)
{
// write code here
let head1 = new ListNode(0);
head1.next = pHead1;
while (pHead1) {
pHead1.flag = 1;
pHead1 = pHead1.next;
}
while (pHead2) {
pHead2.flag = 0;
pHead2 = pHead2.next;
}
while (head1.next) {
if (head1.next.flag == 0) {
return head1.next
}
head1 = head1.next;
}
return null
}
module.exports = {
FindFirstCommonNode : FindFirstCommonNode
};