题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ #include <map> class Solution { public: ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) { std::map<ListNode*, bool> mem; for(auto cur = pHead1; cur; cur = cur->next) { mem.emplace(cur, true); } for(auto cur = pHead2; cur; cur = cur->next) { if(mem.count(cur)) return cur; mem.emplace(cur, true); } return nullptr; } };