From b7c5f2f5ebb6945a036c90b5e245d7138a6013ba Mon Sep 17 00:00:00 2001 From: codingeekks <72185115+codingeekks@users.noreply.github.com> Date: Wed, 5 Oct 2022 18:24:07 +0530 Subject: [PATCH] Create HasCycle.java --- .../Linked_list/java/HasCycle.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 data_structures/Linked_list/java/HasCycle.java diff --git a/data_structures/Linked_list/java/HasCycle.java b/data_structures/Linked_list/java/HasCycle.java new file mode 100644 index 0000000000..59d598b712 --- /dev/null +++ b/data_structures/Linked_list/java/HasCycle.java @@ -0,0 +1,27 @@ +class ListNode { + int val; + ListNode next; + + public ListNode() { + } + + ListNode(int x) { + val = x; + next = null; + } +} +public class HasCycle{ +public boolean HsCycle(ListNode head) { + ListNode fast = head; + ListNode slow = head; + + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + if (fast == slow) { + return true; + } + } + return false; +} +}