From 5b4548dd7807a58dc6e71543c03c537403c5a8d9 Mon Sep 17 00:00:00 2001 From: PAYAL MEHTA <51577440+m-payal@users.noreply.github.com> Date: Mon, 5 Oct 2020 12:56:11 +0530 Subject: [PATCH] Cycle Detection --- Cycle Detection | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Cycle Detection 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; + }