title: Is my implementation of Floyd's cycle detection algorithm incorrect?
body: <p>I have the following code for detecting a cycle in a linked list:</p>
  
  <pre><code>public Class Node {
      Object data;
      Node next = null;
  }
  
  boolean containCycle() {
      boolean retVal = true;
      Node head = this;
      Node slower = head;
      Node faster = head;
  
      if(faster != null &amp;&amp; faster.next != null) {
          faster = faster.next;
      } else {    // there is only one element or zero element
          retVal = false;
      }
  
      if (faster.next != null) {
          faster = faster.next;
      } else {    // there are only 2 elements
          retVal = false;
      }
  
      while (slower != faster &amp;&amp; slower != null &amp;&amp; faster != null) {
          faster = (faster.next != null &amp;&amp; faster.next.next != null) ? faster.next.next : null;
          slower = (slower.next != null) ? slower.next : null;
      }
  
      if (slower == faster) {
          retVal = true;
          System.out.printf("The two pointers meet at: %d\n", faster.data);
      } else {
          retVal = false;
      }
  
      if (retVal) {    // this is the part for detecting where the loop begins
          slower = head;
          while(slower.next != faster.next) {
              slower = slower.next;
              faster = faster.next;
          }
          System.out.println("The cycle starts at: " + slower.data);
      }
  
      return retVal;
  }
  </code></pre>
  
  <p>This code runs fine up until the part where I actually start detecting where the loop begins, which I commented in the code. Somehow, this runs into an infinite loop.</p>
  
  <p>I suspect that this is somehow related to pass by reference in Java? Am I updating the value that <code>head</code> refers to while I was detecting the loop? I am really out of ideas here. Please help!</p>