题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
//set,唯一,count()只能是0or1;mulitset 不唯一,二者应用红黑树
class Solution {
public:
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
ListNode* pTail1=pHead1;
ListNode* pTail2=pHead2;
multiset< ListNode*>Hash;
while (pTail1)
{
Hash.insert(pTail1);
pTail1 = pTail1->next;
}
while (pTail2)
{
Hash.insert(pTail2);
while (Hash.count(pTail2)==2)
{
return pTail2;
}
pTail2 = pTail2->next;
}
return nullptr;
}
};
查看15道真题和解析
