/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null){
            return false;
        }
        //设置两个指针,一个一步走一个节点,一个一步走两个节点,如果有环,二者会相等的时候,如果无环,二者会有一个先为null
        ListNode first = head;
        ListNode second = head;
        do {
            first = first.next;
            if(second.next != null){
                second = second.next.next;
            }else {
                return false;
            }
        }while (first != null && second != null && first != second);
        //步频不同,如果有一个为null 肯定不等
        return first == second;
    }
}