// https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem

class Node {
    int data;
    Node next;
}

public class CycleDetection {
    boolean hasCycle(Node head) {
        if (head == null)
            return false;

        HashSet set = new HashSet();
        Node node = head;
        set.add(node);
        int count = 0;

        while (node.next != null) {
            node = node.next;

            if (set.contains(node)) return true;
            else set.add(node);

            if (++count >101)
                break;
        }

        return false;
    }
}