Clean Java solution with explanation


  • 1
    public ListNode detectCycle(ListNode head) {
      ListNode slow = head, fast = head;
      
      while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        
        if (slow == fast)
            // found the cycle
            break;
      }
      
      if (fast == null || fast.next == null)
        // in case there is no cycle
        return null;
      
      // let the slow pointer go from the head 
      // and meet the fast pointer
      // the meeting point is our answer
      slow = head;
      
      while (slow != fast) {
        slow = slow.next;
        fast = fast.next;
      }
      
      return slow;
    }

Log in to reply
 

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