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; +} +}