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.
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:
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
- 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 email@example.com. 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.