题解 | #链表中环的入口结点#C++暴力链表哈希表解法
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
unordered_map<ListNode*,int>hash;
ListNode*p=pHead;
while(p)
{
if(hash.count(p)==0)
{
hash[p]=1;
}
else
{
hash[p]++;
}
if(hash[p]==2)
{
return p;
}
p=p->next;
}
return NULL;
}
};
