A short java solution using fast runner


  • 2
    G
        public boolean hasCycle(ListNode head) {
        if(head==null)return false;
        ListNode runner = head;
        ListNode p=head;
        while(p!=null&&runner!=null&&runner.next!=null){
            p=p.next;
            runner=runner.next.next;
            if(p==runner)return true;
        }
        return false;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.