/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
unordered_set<ListNode *> my;
while(pHead ){
//1.方法1
if(my.find(pHead) != my.end())
return pHead;
my.insert(pHead);
pHead = pHead->next;
//2.方法2,两者都是用hash的原理实现
// if(my.count(pHead)) return pHead;
//my.insert(pHead);
// pHead = pHead->next;
}
return nullptr;
}
};

京公网安备 11010502036488号