Linked List Cycle II Solution LEET code
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow=head;
ListNode fast =head;
while(fast!=null && fast.next!=null )
{
slow=slow.next;
fast=fast.next.next;
if(slow==fast)
{
ListNode n =head;
while(n!=slow)
{
slow=slow.next;
n=n.next;
}
return n;
}
}
return null;
}
}
Comments
Post a Comment