Skip to content

Commit b08611c

Browse files
committed
FloydDetectLoop Question
1 parent f3b6c8d commit b08611c

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<iostream>
2+
using namespace std;
3+
struct Node{
4+
int data;
5+
Node * next;
6+
Node(int val) : data(val), next(nullptr) {}
7+
};
8+
bool isCircularLinkedList(Node * head){
9+
// if linked list is empty
10+
if(head == nullptr){
11+
return true;
12+
}
13+
// if one node is present
14+
Node * temp = head->next;
15+
while(temp != nullptr && temp != head){
16+
temp = temp->next;
17+
}
18+
if(temp == head){
19+
return true;
20+
}
21+
return false;
22+
}
23+
int main(){
24+
Node * head = new Node(2);
25+
head->next = new Node(3);
26+
head->next->next = new Node(4);
27+
head->next->next->next = head;
28+
if(isCircularLinkedList(head)){
29+
cout << "Linked List is circular in Nature " << endl;
30+
}
31+
else{
32+
cout << "Linked list is not circular in nature " << endl;
33+
}
34+
return 0;
35+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include<iostream>
2+
using namespace std;
3+
struct Node{
4+
int data;
5+
Node * next;
6+
Node(int val): data(val), next(nullptr) {}
7+
};
8+
Node* floydDetectLoop(Node* head) {
9+
if(head == NULL)
10+
return NULL;
11+
Node* slow = head;
12+
Node* fast = head;
13+
while(slow != NULL && fast !=NULL) {
14+
fast = fast -> next;
15+
if(fast != NULL) {
16+
fast = fast -> next;
17+
}
18+
slow = slow -> next;
19+
if(slow == fast) {
20+
return slow;
21+
}
22+
}
23+
return NULL;
24+
}
25+
Node* getStartingNode(Node* head) {
26+
if(head == NULL)
27+
return NULL;
28+
Node* intersection = floydDetectLoop(head);
29+
if(intersection == NULL)
30+
return NULL;
31+
Node* slow = head;
32+
while(slow != intersection) {
33+
slow = slow -> next;
34+
intersection = intersection -> next;
35+
}
36+
return slow;
37+
}
38+
Node *removeLoop(Node *head)
39+
{
40+
if( head == NULL)
41+
return NULL;
42+
Node* startOfLoop = getStartingNode(head);
43+
if(startOfLoop == NULL)
44+
return head;
45+
Node* temp = startOfLoop;
46+
while(temp -> next != startOfLoop) {
47+
temp = temp -> next;
48+
}
49+
temp -> next = NULL;
50+
return head;
51+
}
52+
int main(){
53+
Node * head = new Node(1);
54+
head->next = new Node(2);
55+
head->next->next = new Node(3);
56+
head->next->next->next = new Node(4);
57+
head->next->next->next->next = new Node(5);
58+
head->next->next->next->next->next = head->next; // Creating a loop for testing
59+
cout << "Original Linked List (with loop): ";
60+
Node* temp = head;
61+
for(int i = 0; i < 7; ++i) { // Print a few nodes to show the loop
62+
if(temp == nullptr) break;
63+
cout << temp->data << " ";
64+
temp = temp->next;
65+
}
66+
cout << "..." << endl;
67+
Node* loopStart = getStartingNode(head);
68+
if(loopStart != nullptr){
69+
cout << "Loop starts at node with data: " << loopStart->data << endl;
70+
} else {
71+
cout << "No loop detected." << endl;
72+
}
73+
head = removeLoop(head);
74+
cout << "Linked List after removing loop: ";
75+
temp = head;
76+
while(temp != nullptr){
77+
cout << temp->data << " ";
78+
temp = temp->next;
79+
}
80+
cout << endl;
81+
return 0;
82+
}

Linked_List/detect_loop.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<iostream>
2+
using namespace std;
3+
#include<map>
4+
#include<vector>
5+
struct Node{
6+
int data;
7+
Node * next;
8+
Node(int val) : data(val), next(nullptr) {}
9+
};
10+
bool detectLoop(Node * head){
11+
if(head == nullptr){
12+
return false;
13+
}
14+
map<Node*, bool> visited;
15+
Node * temp = head;
16+
while(temp != nullptr){
17+
if(visited[temp] == true){
18+
return true;
19+
}
20+
visited[temp] = temp->next;
21+
temp = temp->next;
22+
}
23+
return false;
24+
}
25+
int main(){
26+
Node * head = new Node(2);
27+
head->next = new Node(3);
28+
head->next->next = new Node(4);
29+
head->next->next->next = new Node(5);
30+
head->next->next->next->next = head->next;
31+
if(detectLoop(head)){
32+
cout << "We detect a loop in the linked list. " << endl;
33+
}
34+
else{
35+
cout << "We do not detect a loop in a linked list " << endl;
36+
}
37+
return 0;
38+
}

Linked_List/floydcycledetect.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<iostream>
2+
using namespace std;
3+
struct Node{
4+
int data;
5+
Node * next;
6+
Node(int val) : data(val), next(nullptr) {}
7+
};
8+
Node * floydDetectLoop(Node* head){
9+
if(head == nullptr){
10+
return nullptr;
11+
}
12+
Node * slow = head;
13+
Node * fast = head;
14+
while(slow != nullptr && fast != nullptr){
15+
fast = fast->next;
16+
if(fast != nullptr){
17+
fast = fast->next;
18+
}
19+
slow = slow->next;
20+
if(slow == fast){
21+
return slow;
22+
}
23+
}
24+
return nullptr;
25+
}
26+
int main(){
27+
Node * head = new Node(1);
28+
head->next = new Node(2);
29+
head->next->next = new Node(3);
30+
head->next->next->next = new Node(4);
31+
head->next->next->next->next = new Node(5);
32+
head->next->next->next->next->next = head->next;
33+
Node * loop = floydDetectLoop(head);
34+
if(loop != nullptr){
35+
cout << "We detect a loop in the linked list. " << endl;
36+
}
37+
else{
38+
cout << "We do not detect a loop in a linked list. " << endl;
39+
}
40+
return 0;
41+
}

0 commit comments

Comments
 (0)