LeetCode141 链表的环

https://leetcode.com/problems/linked-list-cycle/

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

猜你喜欢

转载自blog.csdn.net/fruit513/article/details/85061365