
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{
public:
    bool hasCycle(ListNode *head)
    {
        ListNode *low = head;   //慢指针
        ListNode *quick = head; //快指针
        while (low && quick && quick->next)
        {
            low = low->next;
            quick = quick->next->next;
            if (low == quick)
            {
                return true;
            }
        }
        return false;
    }
};