package leetcode;

/**
 * https://leetcode.com/problems/linked-list-cycle-ii/
 */
public class Problem142 {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode detectCycle(ListNode head) {
        return null;
    }
    
    public static void main(String[] args) {
        Problem142 prob = new Problem142();
        
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node3;
        
        System.out.println(prob.detectCycle(node1).val);
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -369,117 +369,41 @@
     
-return null;%0A    %7D%0A    %0A    public static void main(String%5B%5D args) %7B%0A        Problem142 prob = new Problem142
+Set%3CListNode%3E set = new HashSet%3C%3E
 ();%0A
@@ -410,25 +410,21 @@
         
-%0A        
+for (
 ListNode
@@ -432,196 +432,88 @@
 node
-1
  = 
-new ListNode(1);%0A        ListNode
+head;
  node
-2
  
+!
 = n
-ew ListNode(2);%0A        ListN
+ull; n
 ode
+ =
  node
-3 = new ListNode(3);%0A        ListNode node4 = new ListNode(4);%0A        ListNode node5 = new ListNode(5);
+.next) %7B%0A            if (set.contains(node)) %7B
 %0A   
@@ -517,25 +517,24 @@
         
-%0A
         
 node1.ne
@@ -529,26 +529,19 @@
     
-node1.next =
+return
  node
-2
 ;%0A  
@@ -550,110 +550,39 @@
     
-node2.next = node3;%0A        node3.next = node4;%0A        node4.next = node5;%0A        node5.next = 
+    %7D%0A            set.add(
 node
-3
+)
 ;%0A  
@@ -587,16 +587,17 @@
         
+%7D
 %0A       
@@ -601,55 +601,19 @@
     
-System.out.println(prob.detectCycle(node1).val)
+return null
 ;%0A  

Solve problem 142