import java.lang.*; import java.util.*; import node.*; class ListCycle { public static boolean hasCycle(ListNode head) { if (head == null) return false; else if (head.next == null) return false; ListNode slow = head; ListNode fast = head.next; while (slow != null && fast.next != null) { if (slow == fast) return true; slow = slow.next; fast = fast.next.next; } return false; } public static void main(String[] args) { ListNode first = new ListNode(2, null); ListNode third = new ListNode(4, first); ListNode second = new ListNode(0, third); first.next = second; ListNode head = new ListNode(3, first); System.out.println(hasCycle(head)); } }