题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
//当快慢指针相遇后,让slow继续移动,同时让head开始移动,根据两变量的关系pHead与slow
//相遇的时候pHead为环的入口点
struct ListNode* EntryNodeOfLoop(struct ListNode* pHead ) {
struct ListNode* slow=pHead;
struct ListNode* fast=pHead;
while(fast!=NULL&&fast->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
if(fast==slow)
{
while(slow!=pHead)
{
slow=slow->next;
pHead=pHead->next;
}
return pHead;
}
}
return NULL;
}