141. Linked List Cycle

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章