Python Program For Finding The Length Of Loop In Linked List Last Updated : 18 May, 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 Python3 # Python program to detect a loop and # find the length of the loop # Node defining class class Node: # Function to make a node def __init__(self, val): self.val = val self.next = None # Linked List defining and loop # length finding class class LinkedList: # Function to initialize the # head of the linked list def __init__(self): self.head = None # Function to insert a new # node at the end def AddNode(self, val): if self.head is None: self.head = Node(val) else: curr = self.head while(curr.next): curr = curr.next curr.next = Node(val) # Function to create a loop in the # Linked List. This function creates # a loop by connecting the last node # to n^th node of the linked list, # (counting first node as 1) def CreateLoop(self, n): # LoopNode is the connecting node to # the last node of linked list LoopNode = self.head for _ in range(1, n): LoopNode = LoopNode.next # end is the last node of the # Linked List end = self.head while(end.next): end = end.next # Creating the loop end.next = LoopNode # Function to detect the loop and return # the length of the loop if the returned # value is zero, which means that either # the linked list is empty or the linked # list doesn't have any loop def detectLoop(self): # If linked list is empty then there # is no loop, so return 0 if self.head is None: return 0 # Using Floyd’s Cycle-Finding # Algorithm/ Slow-Fast Pointer Method slow = self.head fast = self.head # To show that both slow and fast # are at start of the Linked List flag = 0 while(slow and slow.next and fast and fast.next and fast.next.next): if slow == fast and flag != 0: # Means loop is confirmed in the # Linked List. Now slow and fast # are both at the same node which # is part of the loop count = 1 slow = slow.next while(slow != fast): slow = slow.next count += 1 return count slow = slow.next fast = fast.next.next flag = 1 return 0 # No loop # Setting up the code # Making a Linked List and # adding the nodes myLL = LinkedList() myLL.AddNode(1) myLL.AddNode(2) myLL.AddNode(3) myLL.AddNode(4) myLL.AddNode(5) # Creating a loop in the linked List # Loop is created by connecting the # last node of linked list to n^th node # 1<= n <= len(LinkedList) myLL.CreateLoop(2) # Checking for Loop in the Linked List # and printing the length of the loop loopLength = myLL.detectLoop() if myLL.head is None: print("Linked list is empty") else: print(str(loopLength)) # This code is contributed by _Ashutosh 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 Python Program For Finding The Length Of Loop In Linked List K kartik Follow Improve Article Tags : Python Linked Lists Adobe Qualcomm Python-DSA +1 More Practice Tags : AdobeQualcommpython 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 How to Find Length of a list in Python The length of a list means the number of elements it contains. In-Built len() function can be used to find the length of an object by passing the object within the parentheses. Here is the Python example to find the length of a list using len().Pythona1 = [10, 50, 30, 40] n = len(a1) print("Size of 2 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 Operator.length_hint() method in Python length_hint() method is part of the operator module, which contains a collection of functions that correspond to standard operations and can be useful for functional programming and optimization.How operator.length_hint() WorksThe length_hint() function is designed to interact with objects that impl 2 min read Implementation of XOR Linked List in Python Prerequisite: XOR Linked List An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory-efficient version of Doubly Linked List can be created using only one space for the address field with every node. This memory efficient Doub 6 min read Like