From 4d5b5d8b615fb86c7afda5a7121d81a929df8a63 Mon Sep 17 00:00:00 2001 From: Swethashub-source <73125287+Swethashub-source@users.noreply.github.com> Date: Wed, 28 Jul 2021 00:11:56 +0530 Subject: [PATCH 1/4] Create Linked List Cycle --- Linked List/Linked List Cycle | 141 ++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Linked List/Linked List Cycle diff --git a/Linked List/Linked List Cycle b/Linked List/Linked List Cycle new file mode 100644 index 0000000..16adc8d --- /dev/null +++ b/Linked List/Linked List Cycle @@ -0,0 +1,141 @@ +// C++ program to detect loop in a linked list +//Floyd’s Cycle-Finding Algorithm + +#include +#include +using namespace std; + +/* Link list node */ +typedef struct Node { + int data; + struct Node* next; +}ListNode; + +void push(ListNode** head_ref, int new_data) +{ + /* allocate node */ + ListNode* new_node = new Node; + + /* put in the data */ + new_node->data = new_data; + + /* link the old list off the new node */ + new_node->next = (*head_ref); + + /* move the head to point to the new node */ + (*head_ref) = new_node; +} + +class Solution { +public: + bool hasCycle(ListNode *head) { + + //Take two pointers slow and fast + + ListNode *current_pointer=head; + + ListNode *ahead_pointer=head; + + //Our slow pointer moves with us and fast pointer moves fast to check for loop or null address + + //Our loop runs untill current,ahead and ahead->head which may mark the loop, are not null + + while(current_pointer && ahead_pointer && ahead_pointer->next){ + + current_pointer=current_pointer->next; // we move current pointer in 1 step + + ahead_pointer=ahead_pointer->next->next; // we move ahead pointer in two steps + + if(current_pointer==ahead_pointer) return true; // loop is detected when both point to the same address + + } + // in this algorithm ahead pointer moves over the same addresses for few times to detect the loop since current pointer is just at 1 step movement + + return false;// when loop ends and reaches till here it means there might be null address encountered and no loop + } +}; +// Returns true if there is a loop in linked list +// else returns false. + +/* Main program to test our function*/ +int main() +{ + /* Start with the empty linked list */ + ListNode* head = NULL; + int data; + //"Enter some data to continue and 0 to stop"; + cin>>data; + while(data!=0){ + push(&head, data); + cin>>data; + } + + /* Create a loop for testing so try to give input atleast with 4 nodes else loop can't be created*/ + head->next->next->next->next = head; + Solution *ob; + + if (ob->hasCycle(head)) + cout << "Loop found"; + else + cout << "No Loop"; + + return 0; +} + +/* +Ex-1: + +3->2->0->4->2 + +here loop is from 4 to 2 back + +current and ahead mark to head i.e;3 +Now, +in loop +current(not null) and ahead(not null) and ahead->next(2)(not null) +current=2 +ahead=0 + +current(not null) and ahead(not null) and ahead->next(4)(not null) +current=0 +ahead=2 + +current(not null) and ahead(not null) and ahead->next(0)(not null) +current=4 +ahead=4 + +here, +current==ahead =>loop is detected.Return true + +Ex-2: + +1->2->3->4->5->2 + +here loop is from 5 to 2 back + +current and ahead mark to head i.e;1 +Now, +in loop +current(not null) and ahead(not null) and ahead->next(2)(not null) +current=2 +ahead=3 + +current(not null) and ahead(not null) and ahead->next(4)(not null) +current=3 +ahead=5 + +current(not null) and ahead(not null) and ahead->next(2)(not null) +current=4 +ahead=3 + +current(not null) and ahead(not null) and ahead->next(4)(not null) +current=5 +ahead=5 + +here, +current==ahead =>loop is detected.Return true + +**Assume above input as addresses with data as the given numbers +*/ +//Time Complexity : O(n) where n can be the length of the loop made from the head pointer +//Space Complexity : O(1) From 9e9117e4f263a2940660628a87a5a6fde81a9f6c Mon Sep 17 00:00:00 2001 From: Swethashub-source <73125287+Swethashub-source@users.noreply.github.com> Date: Wed, 28 Jul 2021 23:47:40 +0530 Subject: [PATCH 2/4] Rename Linked List/Linked List Cycle to C++/6_Linked List Cycle --- Linked List/Linked List Cycle => C++/6_Linked List Cycle | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Linked List/Linked List Cycle => C++/6_Linked List Cycle (100%) diff --git a/Linked List/Linked List Cycle b/C++/6_Linked List Cycle similarity index 100% rename from Linked List/Linked List Cycle rename to C++/6_Linked List Cycle From 17d782c10b09e6cd20ff568a1b7201809e9e1eec Mon Sep 17 00:00:00 2001 From: Swethashub-source <73125287+Swethashub-source@users.noreply.github.com> Date: Thu, 29 Jul 2021 13:15:38 +0530 Subject: [PATCH 3/4] Rename 6_Linked List Cycle to 6_Linked List Cycle. cpp --- C++/{6_Linked List Cycle => 6_Linked List Cycle. cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C++/{6_Linked List Cycle => 6_Linked List Cycle. cpp} (100%) diff --git a/C++/6_Linked List Cycle b/C++/6_Linked List Cycle. cpp similarity index 100% rename from C++/6_Linked List Cycle rename to C++/6_Linked List Cycle. cpp From 797a03fd6f68ed7be9928973e28e467bf7ed4fca Mon Sep 17 00:00:00 2001 From: Swethashub-source <73125287+Swethashub-source@users.noreply.github.com> Date: Thu, 29 Jul 2021 16:47:08 +0530 Subject: [PATCH 4/4] Rename 6_Linked List Cycle. cpp to 6_Linked List Cycle.cpp --- C++/{6_Linked List Cycle. cpp => 6_Linked List Cycle.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C++/{6_Linked List Cycle. cpp => 6_Linked List Cycle.cpp} (100%) diff --git a/C++/6_Linked List Cycle. cpp b/C++/6_Linked List Cycle.cpp similarity index 100% rename from C++/6_Linked List Cycle. cpp rename to C++/6_Linked List Cycle.cpp