diff --git a/src/main/java/com/thealgorithms/LinkedList/TortoiseAndHare.java b/src/main/java/com/thealgorithms/LinkedList/TortoiseAndHare.java new file mode 100644 index 000000000000..2ed51d7e3629 --- /dev/null +++ b/src/main/java/com/thealgorithms/LinkedList/TortoiseAndHare.java @@ -0,0 +1,30 @@ +package com.thealgorithms.LinkedList; + +/** + * Detects a cycle in a singly linked list using Tortoise and Hare algorithm + */ +public class TortoiseAndHare { + + static class ListNode { + int val; + ListNode next; + + ListNode(int x) { + this.val = x; + next = null; + } + } + + public boolean hasCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + + if (slow == fast) return true; + } + return false; + } +}