package ru.aserdyuchenko.list.task_5_3_4;

import java.util.LinkedList;
import java.util.List;

/**
 * @author anton.
 * @param <T> - T.
 */
public class Node<T> {
    /**
     * @param value - value.
     */
    private T value;
    /**
     * @param next - next.
     */
    private Node<T> next;

    /**
     * Constructor.
     * @param t - new value.
     */
    Node(T t) {
        this.next = null;
        this.value = t;
    }

    /**
     * Get next.
     * @return - next.
     */
    public Node getNext()  {
        return this.next;
    }

    /**
     * Set next.
     * @param nextValue - next value.
     */
    public void setNext(Node<T> nextValue) {
        this.next = nextValue;
    }

    /**
     * Has cycle.
     * @param first - first node.
     * @return true or false.
     */
    boolean hasCycle(Node first) {
        int count = 0;
        Node newNode = first.getNext();
        while (count < getCountOfElement(first)) {
            if (newNode == null) {
                return false;
            }
            newNode = newNode.getNext();
            count++;
        }
        return true;
    }

    private int getCountOfElement(Node first) {
        List<Node> list = new LinkedList<>();
        Node newNode = first;
        while (newNode != null) {
            if (list.contains(newNode)) {
                return list.size();
            } else {
                list.add(newNode);
                newNode = newNode.getNext();
            }
        }
        return list.size();
    }
}
@@ -849,21 +849,27 @@
    i
-nt count = 0;
+f (first == null) %7B
 %0A   
@@ -877,231 +877,180 @@
     
-Node newNode = first.getNext();%0A        while (count %3C getCountOfElement(first)) %7B%0A            if (newNode == null) %7B%0A                return false;%0A            %7D%0A            newNode = newNode.getNext();%0A            count++;
+    return false;%0A        %7D%0A%0A        Node slow, fast;%0A        slow = fast = first;%0A%0A        while(true) %7B%0A            slow = slow.next;%0A%0A            if(fast.next != null) %7B
 %0A   
@@ -1050,34 +1050,32 @@
 ) %7B%0A        
-%7D%0A
         
 return true;
@@ -1066,224 +1066,140 @@
     
-return true
+fast = fast.next.next
 ;%0A    
-%7D%0A%0A
     
-private int getCountOfElement(Node first) %7B%0A        List%3CNode%3E list = new LinkedList%3C%3E();%0A        Node newNode = first;%0A        while (newNode != null) %7B%0A            if (list.contains(newNode)
+    %7D else %7B%0A                return false;%0A            %7D%0A%0A            if(slow == null %7C%7C fast == null
 ) %7B%0A
@@ -1217,35 +1217,29 @@
      return 
-list.size()
+false
 ;%0A          
@@ -1233,39 +1233,33 @@
 e;%0A            %7D
- else %7B
+%0A
 %0A               
@@ -1259,127 +1259,79 @@
     
-    list.add(newNode);%0A                newNode = newNode.getNext();%0A            %7D%0A        %7D%0A        return list.size();
+if(slow == fast) %7B%0A                return true;%0A            %7D%0A        %7D
 %0A   

Edit task (Check node cycle) #161.