Java rabbit/turtle solution


  • 0
    B

    Can they say O(1) space instead of no extra space which is impossible

    public boolean hasCycle(ListNode head)
    	{
    		ListNode rabbit=head, turtle=head;
    		while(true)
    		{
    			if(rabbit==null || rabbit.next==null || rabbit.next.next==null)
    				return false;
    			if(rabbit.next==turtle || rabbit.next.next==turtle)
    				return true;
    			rabbit=rabbit.next.next;
    			turtle=turtle.next;
    		}
    	}

Log in to reply
 

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