public boolean findIfLoopExists(ListNode head) {
ListNode fastPrt = head;
ListNode slowPtr = head;
while (fastPrt != null && fastPrt.getNext() != null) {
fastPrt = fastPrt.getNext().getNext();
slowPtr = slowPtr.getNext();
if (slowPtr == fastPrt) {
return true;
} else {
return false;
}
}
return false;
}
No comments:
Post a Comment