floyd's loops detection algorithm

PHOTO EMBED

Fri Jul 19 2024 06:47:01 GMT+0000 (Coordinated Universal Time)

Saved by @vishnu_jha ##c++ ##dsa ##linkedlist ##circular ##loop #floyd'sloopdetection ##algorithm

bool floyedDetectionLoop (node* head) {
  if (head == NULL) {
    return false;
  }
  node* fast = head;
  node* slow = head;
  while (fast != NULL && slow != NULL) {
    fast = fast -> next;
    if (fast -> next == NULL) {
      fast = fast -> next;
    }
    slow = slow -> next;
    if (fast == slow) {
      return true;
    }
    
  }
  return false;
}
content_copyCOPY