Java Program For Finding The Length Of Loop In Linked List Last Updated : 05 Apr, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Write a function detectAndCountLoop() that checks whether a given Linked List contains loop and if loop is present then returns count of nodes in loop. For example, the loop is present in below-linked list and length of the loop is 4. If the loop is not present, then the function should return 0. Recommended: Please try your approach on PRACTICE, before moving on to the solution. Approach: It is known that Floyd’s Cycle detection algorithm terminates when fast and slow pointers meet at a common point. It is also known that this common point is one of the loop nodes. Store the address of this common point in a pointer variable say (ptr). Then initialize a counter with 1 and start from the common point and keeps on visiting the next node and increasing the counter till the common pointer is reached again. At that point, the value of the counter will be equal to the length of the loop.Algorithm: Find the common point in the loop by using the Floyd’s Cycle detection algorithmStore the pointer in a temporary variable and keep a count = 0Traverse the linked list until the same node is reached again and increase the count while moving to next node.Print the count as length of loop Java // Java program to count number of nodes // in loop in a linked list if loop is // present import java.io.*; class GFG { // Link list node static class Node { int data; Node next; Node(int data) { this.data =data; next =null; } } // Returns count of nodes present // in loop. static int countNodes( Node n) { int res = 1; Node temp = n; while (temp.next != n) { res++; temp = temp.next; } return res; } /* This function detects and counts loop nodes in the list. If loop is not there in then returns 0 */ static int countNodesinLoop( Node list) { Node slow_p = list, fast_p = list; while (slow_p !=null && fast_p!=null && fast_p.next!=null) { slow_p = slow_p.next; fast_p = fast_p.next.next; /* If slow_p and fast_p meet at some point then there is a loop */ if (slow_p == fast_p) return countNodes(slow_p); } // Return 0 to indicate that there is // no loop return 0; } static Node newNode(int key) { Node temp = new Node(key); return temp; } // Driver code public static void main (String[] args) { Node head = newNode(1); head.next = newNode(2); head.next.next = newNode(3); head.next.next.next = newNode(4); head.next.next.next.next = newNode(5); // Create a loop for testing head.next.next.next.next.next = head.next; System.out.println( countNodesinLoop(head)); } } // This code is contributed by inder_verma. Output: 4 Complexity Analysis: Time complexity:O(n). Only one traversal of the linked list is needed.Auxiliary Space:O(1). As no extra space is required. Please refer complete article on Find length of loop in linked list for more details! Comment More infoAdvertise with us Next Article Javascript Program For Writing A Function To Get Nth Node In A Linked List K kartik Follow Improve Article Tags : Java Linked Lists Adobe Qualcomm Practice Tags : AdobeQualcommJava Similar Reads Find length of loop/cycle in given Linked List Given the head of a linked list. The task is to find the length of the loop in the linked list. If the loop is not present return 0.Examples:Input: head: 1 â 2 â 3 â 4 â 5 â 2Output: 4Explanation: There exists a loop in the linked list and the length of the loop is 4. Input: head: 25 â 14 â 19 â 33 12 min read Program for Nth node from the end of a Linked List Given a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1.Examples:Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list.Input: 35 -> 14 min read Java Program to Find the Length/Size of an ArrayList Given an ArrayList in Java, the task is to write a Java program to find the length or size of the ArrayList. Examples: Input: ArrayList: [1, 2, 3, 4, 5] Output: 5 Input: ArrayList: [geeks, for, geeks] Output: 3 ArrayList - An ArrayList is a part of the collection framework and is present in java.uti 2 min read Javascript Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1 -> 10 -> 30 -> 14, index = 2Output: 30 Explanation: The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, befo 4 min read How to Add Element at First and Last Position of LinkedList in Java? LinkedList is a part of Collection framework present inside java.util package. This class is an implementation of LinkedList data structure which is a linear data structure where the elements are not stored in a contiguous manner and every element is a separate object with a data field and address f 2 min read Find Middle of the Linked List Given a singly linked list, the task is to find the middle of the linked list. If the number of nodes are even, then there would be two middle nodes, so return the second middle node.Example:Input: linked list: 1->2->3->4->5Output: 3 Explanation: There are 5 nodes in the linked list and 14 min read Like