You are viewing a single comment's thread. Return to all comments →
Did something similar but just changed around the checks, unfortunatley I keep hitting a null pointer and I'm unsure why. Any thoughts?
boolean hasCycle(Node head) {
if(head==null){ return false; }
Node Fast = head.next; Node Slow = head;
while(Fast.next != null || Fast != null){
if(Fast == Slow){ return true; } Fast = Fast.next.next; Slow = Slow.next;
}
return false;
P.S. Unfortunatley I can't seem to remember how to set which language I'm using on my comment
Seems like cookies are disabled on this browser, please enable them to open this website
Linked Lists: Detect a Cycle
You are viewing a single comment's thread. Return to all comments →
Did something similar but just changed around the checks, unfortunatley I keep hitting a null pointer and I'm unsure why. Any thoughts?
boolean hasCycle(Node head) {
Node Fast = head.next; Node Slow = head;
while(Fast.next != null || Fast != null){
}
return false;
}
P.S. Unfortunatley I can't seem to remember how to set which language I'm using on my comment