题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* EntryNodeOfLoop(ListNode* pHead) { ListNode* fast = pHead; ListNode* slow = pHead; bool is_loop = false; if (!pHead || !pHead->next) { return nullptr; } while (fast && fast->next->next) { fast = fast->next->next; slow = slow->next; if (fast == slow) { is_loop = true; break; } } if (!is_loop) { return nullptr; } ListNode* slow_2 = pHead; while (slow_2 != slow) { slow_2 = slow_2->next; slow = slow->next; } return slow; } };