forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlengthOfLoop.cpp
More file actions
86 lines (75 loc) · 1.69 KB
/
lengthOfLoop.cpp
File metadata and controls
86 lines (75 loc) · 1.69 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// C++ program to count number of nodes
// in loop in a linked list if loop is
// present
#include<bits/stdc++.h>
using namespace std;
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
// Returns count of nodes present in loop.
int countNodes(struct Node *n)
{
int res = 1;
struct Node *temp = n;
while (temp->next != n)
{
res++;
temp = temp->next;
}
return res;
}
/* This function detects and counts loop
nodes in the list. If loop is not there
in then returns 0 */
int countNodesinLoop(struct Node *head)
{
struct Node* fast= head;// minimum 2 node
struct Node* slow = head; // minimum 3 node
//end when fast and slow are equal
bool loopExists=false;
while(fast->next && fast->next->next){
fast=fast->next->next;
slow=slow->next;
if(fast==slow){
loopExists=true;
break;
}
}
if(fast==slow && loopExists){
Node* temp=fast;
int count=0;
while(temp->next!=fast){
temp=temp->next;
count++;
}
return count+1;
}
else{
return 0;
}
}
struct Node *newNode(int key)
{
struct Node *temp =
(struct Node*)malloc(sizeof(struct Node));
temp->data = key;
temp->next = NULL;
return temp;
}
// Driver Code
int main()
{
struct Node *head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
/* Create a loop for testing */
head->next->next->next->next->next = head->next;
cout << countNodesinLoop(head) << endl;
return 0;
}
// This code is contributed by SHUBHAMSINGH10