题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
利用哈希表set()判断节点是否出现过
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
unordered_set<ListNode*> pH;
ListNode *cur = pHead1;
while(cur){
pH.insert(cur);
cur = cur->next;
}
cur = pHead2;
while(cur){
if(pH.find(cur) != pH.end()){
return cur;
}
cur = cur->next;
}
return nullptr;
}
};