From 82d82bee47be420e0d7722b97f85b534f7eacde0 Mon Sep 17 00:00:00 2001 From: HananHindy Date: Wed, 9 Jan 2019 00:09:37 +0200 Subject: [PATCH] Special case handling Fix issue with cycle to the head node --- chapter-2-Linked-Lists/2-8-loop-detection.cpp | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/chapter-2-Linked-Lists/2-8-loop-detection.cpp b/chapter-2-Linked-Lists/2-8-loop-detection.cpp index f9fcd18..c9f889b 100644 --- a/chapter-2-Linked-Lists/2-8-loop-detection.cpp +++ b/chapter-2-Linked-Lists/2-8-loop-detection.cpp @@ -18,10 +18,23 @@ void removeLoop( Node * loopNode, Node * head ) { Node * ptr1 = head; Node * ptr2 = loopNode; - while ( ptr1->next != ptr2->next ) { - ptr1 = ptr1->next; - ptr2 = ptr2->next; + + //In case of full cycle, should move to the final node and terminate the loop from there. + //Otherwise, the head node will be seperated from the rest of the list + if (loopNode == head) + { + while (ptr2->next != head) { + ptr2 = ptr2->next; + } + } + else + { + while (ptr1->next != ptr2->next) { + ptr1 = ptr1->next; + ptr2 = ptr2->next; + } } + //ptr2 has reached start of loop, now removing the loop. ptr2->next = nullptr; } @@ -83,6 +96,10 @@ int main() printList( head ); std::cout << "Inserting loop, connecting 5 to 2 \n"; head->next->next->next->next->next = head->next; + // to test the special case, replace the loop with + // std::cout << "Inserting loop, connecting 5 to 1 \n"; + // head->next->next->next->next->next = head; + std::cout << "Detecting and deleting loop\n"; detectAndRemoveCycle(head); std::cout << "Back to the same old list\n";