-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseventh.cpp
53 lines (53 loc) · 1.19 KB
/
seventh.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
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* head;
void detectLoop() {
int count = 1;
node* slow_t = head;
node* fast_t = head;
do{
slow_t = slow_t->next;
fast_t = fast_t->next->next;
}while (slow_t != fast_t);
slow_t = head;
while (slow_t != fast_t) {
slow_t = slow_t->next;
fast_t = fast_t->next;
count++;
}
std::cout << "The starting point is at node "<< count<<" The value of the node is " <<slow_t->data<< '\n';
}
node* getNewNode(int x)
{
node* temp = new node();
temp->data = x;
temp->next = NULL;
return temp;
}
int main(int argc, char const *argv[]) {
node* temp = getNewNode(5);
node* ptr;
head = temp;
temp->next = getNewNode(10);
temp = temp->next;
temp->next = getNewNode(15);
temp = temp->next;
temp->next = getNewNode(20);
temp=temp->next;
temp->next = getNewNode(25);
temp=temp->next;
ptr = temp;
temp->next = getNewNode(30);
temp=temp->next;
temp->next = getNewNode(35);
temp=temp->next;
temp->next = getNewNode(40);
temp=temp->next;
temp->next = ptr;
detectLoop();
return 0;
}