package ru.matevosyan;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * CycleDetection class.
 * Created on 26.05.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class CycleDetection<E> {

    private int size = 0;


    /**
     * Constructor.
     */

    public CycleDetection() {
    }

    public boolean hasCycle(Node<E> first) {
        Node<E> fast;
        Node<E> slow;
        boolean detForFastToSlow = true;

        if (first != null) {
            fast = first;
            slow = first;
        } else {
            return false;
        }

        while (fast != null) {
            if (detForFastToSlow) {
                fast = fast.next.next;
            } else {
                fast = fast.next;
            }

            if (fast == slow.next) {
                detForFastToSlow = false;
                if (fast == slow) {
                    slow = first;
                }
            }
        }
        return detForFastToSlow;
    }




    /**
     * Class Node describe linkedList entry.
     * @param <E> parameter that defined when create an instance of a class.
     */

    static class Node<E> {
        E item;
        Node<E> next;

        Node(E element, Node<E> next) {
            this.item = element;
            this.next = next;
        }

        public Node(E item) {
            this.item = item;
        }
    }

}
package ru.matevosyan;

/**
 * CycleDetection class.
 * Created on 26.05.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class CycleDetection<E> {

    /**
     * Constructor.
     */

    public CycleDetection() {
    }

    /**
     * Create hasCycle method to check if Linked list have cycle detection.
     * @param first Node from the list.
     * @return true if list have cycle, else return false.
     */

    public boolean hasCycle(Node<E> first) {
        Node<E> fast = first;
        Node<E> slow = first;
        boolean detForFastToSlow = true;

        if (first == null) {
            return false;
        }

        while (fast != null && fast.next != null) {
            if (detForFastToSlow) {
                fast = fast.next.next;
                slow = slow.next;
            } else {
                fast = fast.next;
                slow = slow.next;
                if (fast == slow) {
                    return true;
                }
            }

            if (slow == first) {

                return true;
            } else if (slow == fast){
                detForFastToSlow = false;
                slow = first;
            }
        }
        return false;
    }




    /**
     * Class Node describe linkedList entry.
     * @param <E> parameter that defined when create an instance of a class.
     */

    static class Node<E> {
        E item;
        Node<E> next;

        public Node(E item) {
            this.item = item;
        }
    }

}
change to Floyd's algorithm logic