package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

public class _142 {

    public static class Solution1 {
        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;
        }
    }
}

@@ -78,16 +78,65 @@
 tNode;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 public c
@@ -233,24 +233,1037 @@
 ode head) %7B%0A
+            Set%3CListNode%3E seen = new HashSet%3C%3E();%0A            while (head != null) %7B%0A                if (!seen.add(head)) %7B%0A                    return head;%0A                %7D%0A                head = head.next;%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A    public static class Solution2 %7B%0A        /**%0A         * This comment explains it really well for this solution:%0A         * https://leetcode.com/problems/linked-list-cycle-ii/discuss/44774/Java-O(1)-space-solution-with-detailed-explanation./44281%0A         *%0A         * When fast and slow meet for the first time at point P, fast travelled (a + b + c + b)%0A         * and slow travelled (a + b), and we know fast travels twice fast as slow, so we have:%0A         * a + b + c + b = 2*(a + b), this gives us a == c;%0A         * so at point P, we start a new slow2 pointer from the head, when both slow and slow2 travelled distance a, they must meet%0A         * at cycle entrance point Q.%0A         */%0A        public ListNode detectCycle(ListNode head) %7B%0A
             

add a solution for 142