LinkedList:判断单链表是否有环

2016-05-18  本文已影响0人  敲一手烂代码
public boolean hasCycle(Node head) {
        if(head==null||head.next==null||head.next.next==null) return false;
        Node fast = head.next.next;
        Node slow = head.next;
        while(fast!=slow){
            if(fast.next==null||fast.next.next==null) return false;
            fast = fast.next.next;
            slow = slow.next;
        }
        return true;
    }
上一篇 下一篇

猜你喜欢

热点阅读