/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
bool has_ring(ListNode* pHead){
ListNode *slow = pHead, *fast = pHead;
if(!pHead||!pHead->next||!pHead->next->next) return false;
fast = fast->next->next;
while(slow&&fast->next->next){
if(slow == fast) return true;
slow = slow->next;
fast = fast->next->next;
}
return false;
}
ListNode* EntryNodeOfLoop(ListNode* pHead) {
ListNode *slow = pHead, *fast = pHead;
if(!has_ring(pHead)) return nullptr;
//fast = fast->next->next;
while(slow){
slow = slow->next;
fast = fast->next->next;
if(slow==fast){
fast = pHead;
break;
}
}
while(slow){
if(slow==pHead) return pHead;
if(slow == fast) return slow;
slow = slow->next;
fast = fast->next;
}
return pHead;
}
};