public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null){
return null;
}
ListNode get = getCore(head);
if(get == null){
return null;
}
ListNode set = head;
while(set != get){
set = set.next;
get = get.next;
}
return set;
}
public ListNode getCore(ListNode head){
if(head == null){
return null;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next !=null){
slow = slow .next;
fast = fast.next.next;
if(slow == fast){
return slow;
}
}
return null;
}
}
