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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Run Length Decoding in Linked List
- Check whether the length of given linked list is Even or Odd
- 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 a peak element in Linked List
- Find the product of first k nodes of the given Linked List
- Find the element in a linked list with frequency at least N/3
- Find the middle of a given linked list in C and Java
- Find the product of last N nodes of the given Linked List
- Find the balanced node in a Linked List
- Find the fractional (or n/k - th) node in 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.