My Accepted Solution


  • 0
    K
    public ListNode detectCycle(ListNode head) 
    {
        ListNode current = head;
        ListNode faster = head;
        ListNode slower = head;
        boolean seenBegin = false;
        
        while (faster != null)
        {
            faster = faster.next;
            
            if (faster == null)
            {
                return null;
            }
            
            faster = faster.next;
            
            slower = slower.next;
            
            if (faster == current || slower == current)
            {
                seenBegin = true;
            }
            
            if (faster == slower && seenBegin)
            {
                return current;
            }
            
            if (faster == slower)
            {
                current = current.next;
            }
        }
    
        return null;        
    }

Log in to reply
 

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