Concise JavaSolution O(1)


  • 0
    S
    public boolean hasCycle(ListNode head) {
    	if (head == null)
    		return false;
    	ListNode slow = head;
    	ListNode fast = head.next;
    
    	// move slow node by one unit and fast by two units
    	// if there's a cycle in it then they'll meet eventually
    
    	while (slow != null && fast != null && fast.next != null) {
    
    		if (slow == fast) {
    			return true;
    		}
    		slow = slow.next;
    		fast = fast.next.next;
    	}
    	return false;
    }

Log in to reply
 

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