/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
if(pHead == nullptr)
return nullptr;
ListNode *fast = pHead->next;
ListNode *low = pHead;
//快指针走2步,慢指针走一步
if(fast != nullptr){
low = low->next;
fast = fast->next;
}
else return nullptr;
while(fast != low){
if(fast == nullptr || fast->next == nullptr)
return nullptr;
fast = fast->next->next;
low = low->next;
}
printf("fast:%d low: %d\n",fast->val,low->val);
if(fast == low){
fast = pHead;
while(fast != low)
{
fast = fast->next;
low = low->next;
}
return fast;
}
else return nullptr;
}
};