# Find length of loop in a Linked List using Map

Write a program that checks whether a given Linked List contains loop and if a loop is present then returns the count of nodes in the loop. For example, a loop is present in the below-linked list and the length of the loop is 4. If the loop is not present, then the function should return 0.

**Approach:** In this post, we will use the concept of **Map** to store addresses of nodes present in the linked list as a key and their position as the values.

Below is the step-by-step approach:

- Traverse every node of the linked list and maintain the position starting with one. Increment the position after every node.
- Check whether that node is present in the Map or not.
- If the map does not contain the address of that node, insert it into the map along with its position.
- If the map already contains the address of that node, return the difference between their positions.
- If no such node has been found, return 0.

Below is the implementation of the above approach:

`// C++ program to length of loop ` `// in a linked list using Map ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Linked List node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` ` ` ` ` `Node(` `int` `num) ` ` ` `{ ` ` ` `data = num; ` ` ` `next = NULL; ` ` ` `} ` `}; ` ` ` `// Function detects and counts loop ` `// nodes in the list. If loop is not there, ` `// then returns 0 ` `int` `countNodesinLoop(` `struct` `Node* head) ` `{ ` ` ` `struct` `Node* p = head; ` ` ` `int` `pos = 0; ` ` ` ` ` `// Maintain a map to store addresses ` ` ` `// of node and their position ` ` ` `unordered_map<Node*, ` `int` `> m; ` ` ` ` ` `// Traverse through the linked list ` ` ` `while` `(p != NULL) { ` ` ` ` ` `// If the node is not present in the map ` ` ` `if` `(m.find(p) == m.end()) { ` ` ` `m[p] = pos; ` ` ` `pos++; ` ` ` `} ` ` ` ` ` `// if the node is present ` ` ` `else` `{ ` ` ` ` ` `// Return difference between ` ` ` `// position of the present node and ` ` ` `// position where that node occured before ` ` ` `return` `(pos - m[p]); ` ` ` `} ` ` ` `p = p->next; ` ` ` `} ` ` ` ` ` `// Return 0 to indicate ` ` ` `// there is no loop ` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Create nodes of the linked list ` ` ` `struct` `Node* head = ` `new` `Node(1); ` ` ` `head->next = ` `new` `Node(2); ` ` ` `head->next->next = ` `new` `Node(3); ` ` ` `head->next->next->next = ` `new` `Node(4); ` ` ` `head->next->next->next->next = ` `new` `Node(5); ` ` ` ` ` `// Create a loop for testing the function ` ` ` `head->next->next->next->next->next = head->next; ` ` ` ` ` `// Call the function for the above linked list ` ` ` `cout << countNodesinLoop(head) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Similar Article:** Find the length of a loop in a Linked list using Floyd’s Cycle detection algorithm

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Find length of loop in linked list
- Find first node of loop in a linked list
- Find Length of a Linked List (Iterative and Recursive)
- Detect loop in a linked list
- Detect and Remove Loop in a Linked List
- Make a loop at k-th position in a linked list
- Check linked list with a loop is palindrome or not
- Length of longest palindrome list in a linked list using O(1) extra space
- Check whether the length of given linked list is Even or Odd
- Run Length Decoding in Linked List
- Find intersection point of two Linked Lists without finding the length
- Find sum of even and odd nodes in a linked list
- Find the sum of last n nodes of the given Linked List
- Find extra node in the second Linked list
- Find the product of first k nodes of the given Linked List

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.