|  | 
|  | 1 | +#include <bits/stdc++.h> | 
|  | 2 | +using namespace std; | 
|  | 3 | + | 
|  | 4 | +/* Link list node */ | 
|  | 5 | +struct Node { | 
|  | 6 | +	int data; | 
|  | 7 | +	struct Node* next; | 
|  | 8 | +}; | 
|  | 9 | + | 
|  | 10 | +/* Function to remove loop. */ | 
|  | 11 | +void removeLoop(struct Node*, struct Node*); | 
|  | 12 | + | 
|  | 13 | +/* This function detects and removes loop in the list | 
|  | 14 | +If loop was there in the list then it returns 1, | 
|  | 15 | +otherwise returns 0 */ | 
|  | 16 | +int detectAndRemoveLoop(struct Node* list) | 
|  | 17 | +{ | 
|  | 18 | +	struct Node *slow_p = list, *fast_p = list; | 
|  | 19 | + | 
|  | 20 | +	// Iterate and find if loop exists or not | 
|  | 21 | +	while (slow_p && fast_p && fast_p->next) { | 
|  | 22 | +		slow_p = slow_p->next; | 
|  | 23 | +		fast_p = fast_p->next->next; | 
|  | 24 | + | 
|  | 25 | +		/* If slow_p and fast_p meet at some point then there | 
|  | 26 | +		is a loop */ | 
|  | 27 | +		if (slow_p == fast_p) { | 
|  | 28 | +			removeLoop(slow_p, list); | 
|  | 29 | + | 
|  | 30 | +			/* Return 1 to indicate that loop is found */ | 
|  | 31 | +			return 1; | 
|  | 32 | +		} | 
|  | 33 | +	} | 
|  | 34 | + | 
|  | 35 | +	/* Return 0 to indicate that there is no loop*/ | 
|  | 36 | +	return 0; | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +/* Function to remove loop. | 
|  | 40 | +loop_node --> Pointer to one of the loop nodes | 
|  | 41 | +head --> Pointer to the start node of the linked list */ | 
|  | 42 | +void removeLoop(struct Node* loop_node, struct Node* head) | 
|  | 43 | +{ | 
|  | 44 | +	struct Node* ptr1 = loop_node; | 
|  | 45 | +	struct Node* ptr2 = loop_node; | 
|  | 46 | + | 
|  | 47 | +	// Count the number of nodes in loop | 
|  | 48 | +	unsigned int k = 1, i; | 
|  | 49 | +	while (ptr1->next != ptr2) { | 
|  | 50 | +		ptr1 = ptr1->next; | 
|  | 51 | +		k++; | 
|  | 52 | +	} | 
|  | 53 | + | 
|  | 54 | +	// Fix one pointer to head | 
|  | 55 | +	ptr1 = head; | 
|  | 56 | + | 
|  | 57 | +	// And the other pointer to k nodes after head | 
|  | 58 | +	ptr2 = head; | 
|  | 59 | +	for (i = 0; i < k; i++) | 
|  | 60 | +		ptr2 = ptr2->next; | 
|  | 61 | + | 
|  | 62 | +	/* Move both pointers at the same pace, | 
|  | 63 | +	they will meet at loop starting node */ | 
|  | 64 | +	while (ptr2 != ptr1) { | 
|  | 65 | +		ptr1 = ptr1->next; | 
|  | 66 | +		ptr2 = ptr2->next; | 
|  | 67 | +	} | 
|  | 68 | + | 
|  | 69 | +	// Get pointer to the last node | 
|  | 70 | +	while (ptr2->next != ptr1) | 
|  | 71 | +		ptr2 = ptr2->next; | 
|  | 72 | + | 
|  | 73 | +	/* Set the next node of the loop ending node | 
|  | 74 | +	to fix the loop */ | 
|  | 75 | +	ptr2->next = NULL; | 
|  | 76 | +} | 
|  | 77 | + | 
|  | 78 | +/* Function to print linked list */ | 
|  | 79 | +void printList(struct Node* node) | 
|  | 80 | +{ | 
|  | 81 | +	// Print the list after loop removal | 
|  | 82 | +	while (node != NULL) { | 
|  | 83 | +		cout << node->data << " "; | 
|  | 84 | +		node = node->next; | 
|  | 85 | +	} | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +struct Node* newNode(int key) | 
|  | 89 | +{ | 
|  | 90 | +	struct Node* temp = new Node(); | 
|  | 91 | +	temp->data = key; | 
|  | 92 | +	temp->next = NULL; | 
|  | 93 | +	return temp; | 
|  | 94 | +} | 
|  | 95 | + | 
|  | 96 | +// Driver Code | 
|  | 97 | +int main() | 
|  | 98 | +{ | 
|  | 99 | +	struct Node* head = newNode(50); | 
|  | 100 | +	head->next = newNode(20); | 
|  | 101 | +	head->next->next = newNode(15); | 
|  | 102 | +	head->next->next->next = newNode(4); | 
|  | 103 | +	head->next->next->next->next = newNode(10); | 
|  | 104 | + | 
|  | 105 | +	/* Create a loop for testing */ | 
|  | 106 | +	head->next->next->next->next->next = head->next->next; | 
|  | 107 | + | 
|  | 108 | +	detectAndRemoveLoop(head); | 
|  | 109 | + | 
|  | 110 | +	cout << "Linked List after removing loop \n"; | 
|  | 111 | +	printList(head); | 
|  | 112 | +	return 0; | 
|  | 113 | +} | 
|  | 114 | + | 
|  | 115 | +// This code has been contributed by Striver | 
0 commit comments