题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* phead1, ListNode* phead2) {
unordered_map<ListNode*, int> m;
while(phead1 != nullptr){
m[phead1] = 1;
phead1 = phead1->next;
}
while(phead2 != nullptr){
if(m[phead2] == 1)
return phead2;
m[phead2]++;
phead2 = phead2->next;
}
return nullptr;
}
};


