141. Linked List Cycle

Posted 我的名字叫周周

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了141. Linked List Cycle相关的知识,希望对你有一定的参考价值。

    /*
     * 141. Linked List Cycle 
     * 2016-5-25 by Mingyang
     */
    // 第一个容易犯的错误就是空其实也为fasle,第二个就是条件判断的时候,一定要看fast和fast的下一个都不为空
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast)
                return true;
        }
        return false;
    }

 

以上是关于141. Linked List Cycle的主要内容,如果未能解决你的问题,请参考以下文章