/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
// 双指针
class Solution {
public:
    bool hasCycle(ListNode *head) {

        if(head==nullptr)
        {
            return false;
        }

        // 快慢指针
        ListNode* fast = head;
        ListNode* slow = head;

        //fast每次走两步
        while(fast!=nullptr && fast->next!=nullptr)
        {
            fast = fast->next->next;
            slow = slow->next;
            // 链表是可以判断是否相等!
            if(fast==slow) // 若有环 两者总会相遇
                return true;

        }

        //无环 就一定会碰到null 跳出
        return false;


        
    }
};