check if a loop is present in linked list

PHOTO EMBED

Fri Jul 19 2024 06:37:38 GMT+0000 (Coordinated Universal Time)

Saved by @vishnu_jha ##c++ ##dsa ##linkedlist ##circular ##loop

bool detectLoop (node* head) {
  if (head == NULL) {
    return false;
  }
  map <node*, bool> visited;
  node* temp = head;
  while (temp != NULL) {
    if (visited[temp] == true) {
      return true;
    }
    visited[temp] = true;
    temp = temp -> next;
  }
  return false;
}
content_copyCOPY

https://youtu.be/VxOFflTXlXo?list=PLDzeHZWIZsTryvtXdMr6rPh4IDexB5NIA