遍历第一个链表,将节点存入集合
遍历第二个链表,检查集合中是否有相同的节点
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function FindFirstCommonNode(pHead1, pHead2)
{
// write code here
let set = new Set();
let cur = pHead1;
while(cur){
set.add(cur);
cur = cur.next;
}
let cur2 = pHead2;
while(cur2){
if(set.has(cur2)){
return cur2
}
cur2 = cur2.next;
}
return null;
}
module.exports = {
FindFirstCommonNode : FindFirstCommonNode
};


京公网安备 11010502036488号