You are viewing a single comment's thread. Return to all comments →
In Java..
boolean hasCycle(Node head) { if(head==null || head.next==null) { return false; }
Node pointer1=head; Node pointer2=head;
while(pointer2!=null && pointer1!=null) { if(pointer1==pointer2.next) { return true; } pointer1 = pointer1.next; pointer2 = pointer2.next.next; } return false; }
Seems like cookies are disabled on this browser, please enable them to open this website
Cycle Detection
You are viewing a single comment's thread. Return to all comments →
In Java..
boolean hasCycle(Node head) { if(head==null || head.next==null) { return false; }
Node pointer1=head; Node pointer2=head;
while(pointer2!=null && pointer1!=null) { if(pointer1==pointer2.next) { return true;
} pointer1 = pointer1.next; pointer2 = pointer2.next.next;
}
return false;
}