判断链表是否有环

private boolean slowAndFastSolution(ListNode head) {
    if (head == null) {
        return false;
    }
    ListNode slow = head;
    ListNode fast = head.next;
    while (fast != null && fast.next != null) {
        if (slow.equals(fast)) {
            return true;
        }
        slow = slow.next;
        fast = fast.next.next;
    }

    return false;
}

 

posted @ 2020-08-30 12:57  soft.push("zzq")  Views(83)  Comments(0Edit  收藏  举报