-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetectCycle.cpp
More file actions
21 lines (21 loc) · 1012 Bytes
/
detectCycle.cpp
File metadata and controls
21 lines (21 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<map>
using namespace std;
bool detectLoop(Node *head){
if(head == NULL){
return false;
}
map<Node, bool> Visited;
Node*temp = head;//Visited is a map that has the node as key and value as true or false. The boolean variable will be used to check if it's already been visited before. If so then we can return true, otherwise we'll keep going until we reach the end of the list.
Node * temp = head;//temp is a pointer that points to the node that's currently being checked if it has been visited before or not. If so then we can return true, otherwise we'll keep going until we reach the end of the list.
while(temp!= NULL){
if(Visited[temp]== true){
cou<<"Present on element";//If it's already been visited before then we can return true, otherwise we'll keep going until we reach the end of the list.
return true;
}
visited[temp] = true;
temp = temp-> next;
}
return false;
}
int main(){}