Blogger

Delete comment from: Javarevisited

Hey what if Head is null? That case is not handled!

public static boolean findLoop(Node head) {
boolean retVal = false;
if(head != null) {
Node fst_ptr = head, slw_ptr = head;
while(fst_ptr != null && fst_ptr.next != null) {
fst_ptr = fst_ptr.next.next;
slw_ptr = slw_ptr.next;
if(slw_ptr == fst_ptr) {
retVal = true;
}
}
}
return retVal;
}

This is my code, please let me know where have I been wrong.

The program I have written seems to go in an infinite loop.

Thanks in advance!

Oct 7, 2017, 12:38:34 PM


Posted to How to find If Linked List Contains Loop or Cycle in Java? Example Solution

Google apps
Main menu