https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&tPage=1&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&from=cyc_github&tab=answerKey
假设t时刻相遇
t = a+b
2t = a+n(b+c)+b
a = (n-1)(b+c)+c
slow跑(n-1)(b+c)+c
ans从pHead开始跑a
此时两者一定相遇在入口点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
auto fast = pHead;
auto slow = pHead;
while(fast){
slow = slow->next;
if(fast->next != nullptr){
fast = fast->next->next;
}else{
return nullptr;
}
//有环,开始确定入口点
if(slow == fast){
auto ans = pHead;
while(ans != slow){
ans = ans->next;
slow = slow->next;
}
return ans;
}
}
return nullptr;
}
};