参考思考:https://blog.nowcoder.net/n/deaa284f105e48f49f38b5d7cb809cd7
代码:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
if(pHead==NULL||pHead->next==NULL)
{
return NULL;
}
ListNode* fast = pHead;
ListNode* slow = pHead;
while(fast!=NULL&&fast->next!=NULL)
{
fast = fast->next->next;
slow = slow->next;
if(fast==slow)
{
ListNode* slow2 = pHead;
while(slow2!=slow)
{
slow2 = slow2->next;
slow = slow->next;
}
return slow2;
}
}
return NULL;
}
};
京公网安备 11010502036488号