Java Program For Finding The Length Of Loop In Linked List Last Updated : 05 Apr, 2022 Comments Improve Suggest changes 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 Java Program For Finding The Length Of Loop In Linked List kartik Follow Improve Article Tags : Linked List Java Java Programs DSA Linked Lists Adobe Qualcomm +3 More Practice Tags : AdobeQualcommJavaLinked List Similar Reads Java Program For Finding The Middle Element Of A Given Linked List Given a Singly linked list, find the middle of the linked list. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. Example of Finding Middle Element of Linked ListInput: 1->2->3->4->5 Output: 3Â Input: 1->2->3->4->5-> 6 min read Java Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi 7 min read Java Program To Find The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input: 10->6->8->4->12, n = 2 Output: 16 Sum of last two nodes: 12 + 4 = 16 Input: 15->7->9->5->16->14, n = 4 10 min read Java Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2-> 5 min read Java Program For Checking Linked List With A Loop Is Palindrome Or Not Given a linked list with a loop, the task is to find whether it is palindrome or not. You are not allowed to remove the loop. Examples: Input: 1 -> 2 -> 3 -> 2 /| |/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input: 1 -> 2 -> 3 -> 4 /| |/ ------- 1 4 min read Java Program to Get the First and the Last Element of a Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object whi 4 min read Java Program For Printing Nth Node From The End Of A Linked List Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below the list and n = 3, then the output is "B". Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Method 1 (Use 5 min read Java Program To Find Decimal Equivalent Of Binary Linked List Given a singly linked list of 0s and 1s find its decimal equivalent. Input: 0->0->0->1->1->0->0->1->0 Output: 50 Input: 1->0->0 Output: 4 The decimal value of an empty linked list is considered as 0. Recommended: Please solve it on "PRACTICE" first, before moving on to 2 min read Java Program to Delete a Node From the Ending of the Circular Linked List In this article, we will learn about deleting a node from the ending of a circular linked list. Consider the linked list as shown below:Â Example:Input : 5->3->4->(head node)Output: 5->3->(head node)We will first initialize the list and add some data into it with the addNode() method a 3 min read Java Program for Reverse a linked list Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In 3 min read Like