public ListNode detectCycle(ListNode head) { ListNode fast = head; ListNode slow = head; int flag = 0; ListNode intersection = null; while(fast != null && fast.next != null){ fast = fast.next.next; slow = slow.next; if(fast == slow){ intersection = fast; flag = 1; break; } } if(flag == 0 ) return null; slow = head; while(slow != fast){ slow = slow.next; fast = fast.next; } return fast; }