C++ 代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *findFirstCommonNode(ListNode *headA, ListNode *headB) {
unordered_set<ListNode*> sets;
while(headA){
sets.insert(headA);
headA=headA->next;
}
while(headB){
if(sets.count(headB)>0){
return headB;
}
headB=headB->next;
}
return NULL;
}
};