快慢指针找相遇点,相遇时慢指针从头开始跑,快指针从相遇点继续跑,最终在环的入口处相遇。

/*
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 && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast)
            {
                break;
            }
        }

        if(!fast || !fast->next)
        {
            //没有环退出;
            return NULL;
        }

        //有环的情况下,快指针从相遇的地方继续跑,慢指针从头开始跑;
        //最终在入口点相遇;
        slow = pHead;
        while(fast != slow)
        {
            fast = fast->next;
            slow = slow->next;
        }

        return slow;
    }
};