# Find Length of Loop of the Linked List

“** Find Length of Loop of the Linked List**” problem is an extension of problem “

**”. In this problem, we need to detect loop whether loop exists in a linked list or not, and if loop exists, then we need to find the length of loop of the linked list. In simpler language, we need to count how many nodes are there which are in the loop.**

*Detect Loop in a Linked List*Now, to find the length of the loop of the linked list, firstly, we need to detect whether a loop exist in a linked list or not. If loop exist in the linked list, we need to traverse the entire loop to count the number of nodes in a loop.

For example, in the below linked list, the loop length of loop present in the linked list 3. Nodes with values {2}, {3} and {4} are forming the loop in the below case.

The steps required to find the length of the loop is as follows:

- The very initial step to find the length of loop of the linked list is to check whether loop exists in the linked list or not. To detect whether loop exists in the linked list or not, we have discussed two methods; hashing based and Floyd’s loop detection algorithm. Using any one of these two method, we can detect whether loop exists in the linked list or not.
- If the Loop does not exists in the linked list, then the length of the loop of the linked list will be zero.
- If the Loop exists in the linked list, we will traverse the whole loop and count the number of nodes present in the linked list.
- Finally, we will print the count.

**C++ Program to find length of the loop in a linked list is as follows:**

/* C++ Program to find the length of the loop of the linked list */ #include<bits/stdc++.h> using namespace std; /* Strcuture of the Node of the Linked List */ struct node { int data; struct node *next; }; /* Function to insert node at the end of the linked list */ void insert_end(struct node **head, int ele) { /* Creating a new node */ struct node *new_node; new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = ele; new_node->next = NULL; /* check whether the linked list is empty or not */ /* If Linked List is empty, new node will become head of the linked list */ if(*head == NULL) { *head = new_node; return; } /* If Linked List is not empty, traverse the complete linked list and point the pointer of last node of the linked list to the new node*/ struct node *temp = NULL; temp = (*head); while(temp->next != NULL) temp=temp->next; temp->next = new_node; } /* A Utility function to create Loop in a Linked List */ void create_loop(struct node **head) { struct node *temp = (*head); while(temp->next !=NULL) temp = temp->next; temp->next = (*head)->next; } /* Function to detect loop in a linked list and if loop exists, find the length of the loop */ int detectLoopLength(struct node *head) { /* Initialize two pointers; slow pointer which will traverse the linked list one node at a time and fast pointer which will traverse the linked list two nodes at a time */ struct node *slow = head; struct node *fast = head; while(slow!=NULL && fast!=NULL && fast->next!=NULL) { slow = slow->next; fast = fast->next->next; /* If at any time, the slow pointer and faster pointer meets, then there exists a loop in the linked list */ if(slow == fast) { /* Now, traverse the complete loop and count the number of nodes present in the loop to find the length of the loop of the linked list */ struct node *temp = slow; int len = 1; while(temp->next != slow) { len++; temp = temp -> next; } return len; } } /* If loop does not exists, we will return loop length as zero */ return 0; } int main() { struct node *head; head = NULL; /* Insert Some Nodes in the Linked List */ insert_end(&head,4); insert_end(&head,3); insert_end(&head,2); insert_end(&head,1); /* Calling a utility function to create Loop in the Linked List */ create_loop(&head); /* Calling detectLoopLength function to find the length of the loop of the linked list */ int len = detectLoopLength(head); /* Printing the result */ cout<<"Length of loop present in the linked list is: "<<len; }

Length of loop present in the linked list is: 3OUTPUT: