Detect and Remove Loop in a Linked List in C++In this article, we will discuss how to detect and remove Loop in a Linked List in C++ with different methods. Create a function called detectAndRemoveLoop() that verifies whether a given Linked List has a loop. After that, it removes the Loop and returns true if it does. It returns false if there isn't a loop in the List. A linked list with a loop is depicted in the diagram below. The List below must be changed to 1->2->3->4->5->NULL using the detectAndRemoveLoop() method. Write a C function to detect a loop in a linked list.
Method 1: (Check each one separately)Floyd's Cycle detection process is known to come to an end when the fast and slow pointers collide at the same location. Additionally, we are aware that this common point is one of the loop nodes (2, 3, 4, or 5 in the image above). Put this location in a pointer variable, like ptr2. After that, starting at the head of the Linked List, determine if each node is reachable from ptr2 by checking each one individually. When a node in a linked list is reachable, we can obtain the pointer to the preceding node of that node, indicating that this node represents the beginning of the Loop. Output: Linked List after removing Loop 50 20 15 4 10 Method 2: (Better Solution)
Program: Let us take an example to illustrate how to detect and remove a loop in a linked list in C++. Output: Complexity Analysis: Time Complexity: O(N), Where N is the number of nodes in the tree Auxiliary Space: O(1) Method 3: (Without Counting Nodes in Loop)The number of nodes in the Loop does not need to be counted. After identifying the Loop, if we move the fast and slow pointers at the same speed until the fast pointers don't meet, they will collide at the beginning of the Loop. How does this work? Let Floyd's Cycle detection algorithm lead to a point where slow and fast will collide. The scenario when the cycle is discovered is depicted in the diagram below. We can conclude from the above diagram. From the above equation, we can conclude below As a result, if we start moving both pointers again at the same speed, one pointer (let's call it slow) will start from the head node of the linked List while the other (let's call it fast) will start from the meeting point. The fast pointer would have also gone m steps because they are now going at the same speed when the slow pointer reaches the beginning of the Loop (having made m steps). They would collide at the beginning because the fast pointer starts from k, and m+k is a multiple of n. Program: Output: Complexity Analysis: Time Complexity: O(N), Where N is the number of nodes in the tree Auxiliary Space: O(1) Method 4: Hashing (Hash the linked list nodes' addresses)We can check to see if an element already exists in the map by hashing the addresses of the linked list nodes in an unordered map. We must set the last node's next pointer to NULL because if it exists, we have reached a node that already exists by a cycle. Program: Output: Complexity Analysis: Time Complexity: O(N), Where N is the number of nodes in the tree. Auxiliary Space: O(N), Where N is the number of nodes in the tree (due to hashing).
Next TopicDifference between C++ and Go Languages
|