美文网首页
LinkedList:判断单链表是否有环

LinkedList:判断单链表是否有环

作者: 敲一手烂代码 | 来源:发表于2016-05-18 13:50 被阅读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;
    }

相关文章

网友评论

      本文标题:LinkedList:判断单链表是否有环

      本文链接:https://www.haomeiwen.com/subject/jwrwrttx.html