题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
1、判断是否有环,slow和fast指向链表的开始,slow一次走一步,fast一次走两步, 不带环,fast就为空,带环,fast就会在环里面追上slow 2、一个指针从相遇点开始走,一个指针从链表头开始走,他们会在环的入口点相遇 struct ListNode* EntryNodeOfLoop(struct ListNode* pHead ) { struct ListNode* fast = pHead; struct ListNode* slow = pHead; while(fast && fast->next) { fast = fast->next->next; slow = slow->next; if(fast == slow) { struct ListNode* meet = fast; struct ListNode* head = pHead; while(meet != head) { meet = meet->next; head = head->next; } return meet; } } return NULL; }