package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

 Note: Do not modify the linked list.

 Follow up:
 Can you solve it without using extra space?*/
public class _142 {

    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if ( slow == fast) {
                ListNode slow2 = head;
                while (slow2 != slow) {
                    slow = slow.next;
                    slow2 = slow2.next;
                }
                return slow;
            }
        }
        return null;
    }
}

@@ -81,16 +81,48 @@
 de;%0A%0A/**
+%0A * 142. Linked List Cycle II%0A%0A 
 Given a 
@@ -305,16 +305,18 @@
 a space?
+%0A 
 */%0Apubli
@@ -331,16 +331,50 @@
 _142 %7B%0A%0A
+  public static class Solution1 %7B%0A
     publ
@@ -410,26 +410,24 @@
 ode head) %7B%0A
-  
       ListNo
@@ -444,26 +444,24 @@
 head;%0A      
-  
 ListNode fas
@@ -466,26 +466,24 @@
 ast = head;%0A
-  
       while 
@@ -520,36 +520,32 @@
 null) %7B%0A        
-    
 slow = slow.next
@@ -554,20 +554,16 @@
         
- 
-   
 fast = f
@@ -589,17 +589,12 @@
     
-    
 if (
- 
 slow
@@ -615,22 +615,16 @@
         
- 
-     
 ListNode
@@ -648,22 +648,16 @@
         
-    
-  
 while (s
@@ -680,32 +680,24 @@
             
-      
-  
 slow = slow.
@@ -698,32 +698,24 @@
  slow.next;%0A
-        
             
@@ -740,30 +740,24 @@
 ;%0A          
-      
 %7D%0A          
@@ -756,22 +756,16 @@
         
- 
-     
 return s
@@ -769,20 +769,16 @@
 n slow;%0A
-    
         
@@ -781,30 +781,26 @@
     %7D%0A      
-  %7D%0A  
+%7D%0A
       return
@@ -812,10 +812,14 @@
 ;%0A    %7D%0A
+  %7D%0A
 %7D%0A

refactor 142