将两个链表的长度变成一样
链表1到结尾处继续遍历链表2,链表2到结尾处继续遍历链表1,在遍历的同时比较节点是否相同
function ListNode(x){
this.val = x;
this.next = null;
}
function FindFirstCommonNode(pHead1, pHead2)
{
// write code here
let head1 = pHead1;
let head2 = pHead2;
while (head1 !== head2) {
if (head1 == null) {
head1 = pHead2;
} else {
head1 = head1.next;
}
if (head2 == null) {
head2 = pHead1;
} else {
head2 = head2.next;
}
}
return head1
}
module.exports = {
FindFirstCommonNode : FindFirstCommonNode
};