/* 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; while (fast!=NULL && fast->next != NULL) { fast = fast->next->next; slow = slow->next; if(slow == fast) { // 找到相遇的节点 break; } } if(fast == nullptr || fast->next == nullptr) { return nullptr; } fast = pHead; while(fast != slow) { // 头结点和相遇点到环入口距离一致 fast = fast->next; slow =slow->next; } return fast; } };