diff --git a/Cycle Detection b/Cycle Detection new file mode 100644 index 0000000..8b71110 --- /dev/null +++ b/Cycle Detection @@ -0,0 +1,17 @@ + static boolean hasCycle(SinglyLinkedListNode head) + { + boolean flag = false; + SinglyLinkedListNode slow_p = head, fast_p = head; + + while (slow_p != null && fast_p != null && fast_p.next != null) + { + slow_p = slow_p.next; + fast_p = fast_p.next.next; + if (slow_p == fast_p) + { + flag = true; + break; + } + } + return flag; + }