Here I represents the cycle detection in linkedList using fast and slow pointers.
public class Node{
Object data;
Node next;
}
public Node findCyclePoint(Node head){
if(head==null)
return null;
Node slow = head, fast = head.next;
while(slow!=fast && fast!=null && fast.next!=null){
slow = slow.next; fast = fast.next.next;
}
fast = head;
while(slow!=fast && slow!=null){
slow = slow.next; fast = fast.next;
}
return fast;
}
No comments:
Post a Comment