package com.thealgorithms.linkedlist;
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } }
public class DetectCycle {
public static boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true; // cycle detected
}
}
return false; // no cycle
}
public static void main(String[] args) {
ListNode head = new ListNode(3);
head.next = new ListNode(2);
head.next.next = new ListNode(0);
head.next.next.next = new ListNode(-4);
head.next.next.next.next = head.next; // create a cycle
System.out.println("Cycle exists: " + hasCycle(head));
}
}