-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_detection.cpp
59 lines (49 loc) · 1 KB
/
loop_detection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;
class Node{
public:
Node(){
next = NULL;
}
//void setValue(int value){
// data = value;
//}
//int getValue(){
// return data;
//}
//private:
char data;
Node* next;
};
Node* loop_detection(Node* node){
Node* fr = node->next->next;
Node* sr = node->next;
while(fr != sr){
fr = fr->next->next;
sr = sr->next;
}
sr = node;
while(fr != sr){
fr = fr->next;
sr = sr->next;
}
return sr;
}
int main(){
Node* node = new Node;
node->data = 'A';
node->next = new Node;
node->next->data = 'B';
node->next->next = new Node;
node->next->next->data = 'C';
node->next->next->next = new Node;
node->next->next->next->data = 'D';
node->next->next->next->next = new Node;
node->next->next->next->next->data = 'E';
node->next->next->next->next->next = new Node;
node->next->next->next->next->next->data = 'F';
node->next->next->next->next->next->next = node->next->next->next;
Node* temp = loop_detection(node);
cout << temp->data << endl;
return 0;
}