/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead) {
        ListNode* pSlow = pHead;
        ListNode* pFast = pHead;
        bool hasCycle = false;

        cout << "hasNext:" << (pFast->next == NULL ? 0 : 1) << endl;
        while (pFast && pFast->next) {
            pSlow = pSlow->next;
            pFast = pFast->next->next;

            if (pSlow == pFast) {
                hasCycle = true;
                break;
            }
        }

        cout << "hasCycyle: " << hasCycle << endl;
        if (!hasCycle) {
            return NULL;
        }

        pSlow = pHead;
        while (true) {

            if (pSlow == pFast) {
                return pSlow;
            }

            pSlow = pSlow->next;
            pFast = pFast->next;
        }


        return NULL;
    }
};