方法一: 快慢指针

    private static boolean hasCycle(Node head){
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;

            if (fast == slow) {
                return true;
            }
        }

        return  false;
    }

完整代码



blog comments powered by Disqus

Published

18 December 2012

Tags