1ms simplest Java Solution


  • 0
    L
    public boolean hasCycle(ListNode head) {
            if( head == null) return false;
    
            ListNode fast, slow=fast=head;
    
            while( fast != null && fast.next != null) {
                fast = fast.next.next;  //2x fast
                slow = slow.next;       //1x fast
    
                //If they meet, then has cycle
                if( fast == slow) return true;
            }
    
            return false;
        }
    

Log in to reply
 

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