@@ -0,0 +1,1447 @@
+package data_structures.linked_lists;%0D%0A%0D%0Aimport java.util.ArrayList;%0D%0A%0D%0A/*%0D%0A * Problem Statement:%0D%0A * Youre given the pointer to the head node of a linked list. Find whether the%0D%0A * list contains any cycle (or loop). A linked list is said to contain cycle if%0D%0A * any node is re-visited while traversing the list. The head pointer given may%0D%0A * be null meaning that the list is empty.%0D%0A * %0D%0A * Input Format:%0D%0A * You have to complete the int HasCycle(Node* head) method which takes one%0D%0A * argument - the head of the linked list. You should NOT read any input from%0D%0A * stdin/console. Number of nodes in a linked list doesn't exceed 100.%0D%0A * %0D%0A * Output Format:%0D%0A * Check whether the linked list has a cycle and return 1 if there is a cycle.%0D%0A * Otherwise, return 0. Do NOT print anything to stdout/console.%0D%0A * %0D%0A * Sample Input: %0D%0A * 1 --%3E NULL%0D%0A * %0D%0A * 1 --%3E 2 --%3E 3%0D%0A * %5E %7C%0D%0A * %7C %7C%0D%0A * -----%0D%0A * %0D%0A * Sample Output: %0D%0A * 0%0D%0A * 1%0D%0A */%0D%0Apublic class DetectCycle %7B%0D%0A%0D%0A%09int HasCycle(Node head) %7B%0D%0A%0D%0A%09    if (head == null) %7B%0D%0A%09        return 0;%0D%0A%09    %7D else %7B%0D%0A%09        ArrayList%3CNode%3E array = new ArrayList%3C%3E();%0D%0A%09        Node runner = head;%0D%0A%09        %0D%0A%09        while (runner != null) %7B%0D%0A%09            array.add(runner);%0D%0A%09            for (Node n : array) %7B%0D%0A%09                if (runner.next == n) %7B%0D%0A%09                    return 1;%0D%0A%09                %7D%0D%0A%09            %7D%0D%0A%09            runner = runner.next;%0D%0A%09        %7D%0D%0A%09        return 0;%0D%0A%09    %7D%0D%0A%09%7D%0D%0A%7D

Add DetectCycle