forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed the folder name to Cycle Detection
- Loading branch information
1 parent
e8ea92e
commit 77462c9
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
class SinglyLinkedListNode { | ||
public: | ||
int data; | ||
SinglyLinkedListNode *next; | ||
|
||
SinglyLinkedListNode(int node_data) { | ||
this->data = node_data; | ||
this->next = nullptr; | ||
} | ||
}; | ||
|
||
class SinglyLinkedList { | ||
public: | ||
SinglyLinkedListNode *head; | ||
SinglyLinkedListNode *tail; | ||
|
||
SinglyLinkedList() { | ||
this->head = nullptr; | ||
this->tail = nullptr; | ||
} | ||
|
||
void insert_node(int node_data) { | ||
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data); | ||
|
||
if (!this->head) { | ||
this->head = node; | ||
} else { | ||
this->tail->next = node; | ||
} | ||
|
||
this->tail = node; | ||
} | ||
}; | ||
|
||
void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) { | ||
while (node) { | ||
fout << node->data; | ||
|
||
node = node->next; | ||
|
||
if (node) { | ||
fout << sep; | ||
} | ||
} | ||
} | ||
|
||
void free_singly_linked_list(SinglyLinkedListNode* node) { | ||
while (node) { | ||
SinglyLinkedListNode* temp = node; | ||
node = node->next; | ||
|
||
free(temp); | ||
} | ||
} | ||
|
||
/*The function below detects cycles in a singly linked list. | ||
we have to write the below function in the problem*/ | ||
|
||
bool has_cycle(SinglyLinkedListNode* head) { | ||
SinglyLinkedListNode* fast = head; //iterator to traverse through the linked list. | ||
SinglyLinkedListNode* slow = head; //Another iterator but it will go behind fast iterator. | ||
while(slow != nullptr && fast != nullptr && fast->next!= nullptr) | ||
{ | ||
slow = slow->next ; //slow is going through each node. | ||
fast = fast->next->next; //fast is traversing through skipping each alternate node.Thus ahead of slow. | ||
if(slow==fast) | ||
{ | ||
return true; //if in any step slow and fast is pointing at the same node then cycle exists in the linked list.Hence return true. | ||
} | ||
} | ||
return false; //if loop runs it's life then there is no cycle as fast is either pointing at last element or nullptr. | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
ofstream fout(getenv("OUTPUT_PATH")); | ||
|
||
int tests; | ||
cin >> tests; | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
|
||
for (int tests_itr = 0; tests_itr < tests; tests_itr++) { | ||
int index; | ||
cin >> index; | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
|
||
SinglyLinkedList* llist = new SinglyLinkedList(); | ||
|
||
int llist_count; | ||
cin >> llist_count; | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
|
||
for (int i = 0; i < llist_count; i++) { | ||
int llist_item; | ||
cin >> llist_item; | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
|
||
llist->insert_node(llist_item); | ||
} | ||
|
||
SinglyLinkedListNode* extra = new SinglyLinkedListNode(-1); | ||
SinglyLinkedListNode* temp = llist->head; | ||
|
||
for (int i = 0; i < llist_count; i++) { | ||
if (i == index) { | ||
extra = temp; | ||
} | ||
|
||
if (i != llist_count-1) { | ||
temp = temp->next; | ||
} | ||
} | ||
|
||
temp->next = extra; | ||
|
||
bool result = has_cycle(llist->head); | ||
|
||
fout << result << "\n"; | ||
} | ||
|
||
fout.close(); | ||
|
||
return 0; | ||
} |