解题思路:
利用 STL的set集合 ——set集合元素集合唯一,不存在重复元素。
1.将A链表元素地址依次存入自定义set集合。
2.遍历B链表与set集合进行匹配,放回第一个匹配到的结点
`class Solution {
public:
std::set<ListNode*> myset;
ListNode* FindFirstCommonNode( ListNode* headA, ListNode* headB) {
while(headA)
{
myset.insert(headA);
headA=headA->next;
}
while(headB)
{
if(myset.find(headB)!=myset.end()) return headB;
headB=headB->next;
}
return NULL;
}
};
`