//思路1 class Solution { public: ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) { set<ListNode*> filter; ListNode* res = NULL; while(pHead1 != NULL){ filter.insert(pHead1); pHead1 = pHead1->next; } while(pHead2 != NULL){ if(filter.find(pHead2) != filter.end()){ res = pHead2; break; } pHead2 = pHead2->next; } return res; } }; //思路二 class Solution { public: ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) { ListNode*p1, *p2; p1 = pHead1; p2 = pHead2; while(p1 != p2){ if(p1 != NULL) p1 = p1->next; if(p2 != NULL) p2 = p2->next; if(p1 != p2){ if(p1 == NULL) p1 = pHead2; if(p2 == NULL) p2 = pHead1; } } return p1; } };
原文:https://www.cnblogs.com/chengsheng/p/10678800.html