/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
// 利用哈希表
unordered_set<ListNode*> u_s;
while(pHead)
{
if(u_s.empty() || u_s.count(pHead)==0)
{
u_s.emplace(pHead);
}
else {
return pHead;
break;
}
pHead = pHead->next;
}
return nullptr;
}
};

京公网安备 11010502036488号