首页 > 代码库 > 141. Linked List Cycle

141. Linked List Cycle

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

do-while 的应用

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

  

141. Linked List Cycle