Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop.
We have discussed Floyd’s algorithm to detect cycle in linked list.
Brent’s cycle detection algorithm is similar to floyd’s algorithm as it also uses two pointer technique. But there is some difference in their approaches. Here we make one pointer stationary till every iteration and teleport it to other pointer at every power of two. The start of the cycle is determined by the smallest power of two at which they meet. This improves upon the constant factor of Floyd’s algorithm by reducing the number of calls.
- Move fast pointer (or second_pointer) in powers of 2 until we find a loop. After every power, we reset slow pointer (or first_pointer) to previous value of second pointer. Reset length to 0 after every power.
- The condition for loop testing is first_pointer and second_pointer become same. And loop is not present if second_pointer becomes NULL.
- When we come out of loop, we have length of loop.
- We reset first_pointer to head and second_pointer to node at position head + length.
- Now we move both pointers one by one to find beginning of loop.
Comparison with Floyd’s Algorithm:
1) Finds the length of loop in first cycle detection loop itself. No extra work is required for this.
2) We only move second in every iteration and avoid moving first (which can be costly if moving to next node involves evaluating a function).
C++
// CPP program to implement Brent's cycle // detection algorithm to detect cycle in // a linked list. #include <iostream> using namespace std; /* Link list node */ struct Node { int data; struct Node* next; }; /* This function detects loop in the list If loop was there in the list then it returns, the first node of loop otherwise returns NULL */ struct Node* detectCycle( struct Node* head) { // if head is null then no loop if (head == NULL) return NULL; struct Node* first_pointer = head; struct Node* second_pointer = head->next; int power = 1; int length = 1; // This loop runs till we find the loop. // If there is no loop then second_pointer // ends at NULL . while (second_pointer != NULL && second_pointer != first_pointer) { // condition after which we will // update the power and length as // smallest power of two gives the // start of cycle. if (length == power) { // updating the power. power *= 2; // updating the length length = 0; first_pointer = second_pointer; } second_pointer = second_pointer->next; ++length; } // if it is null then no loop if (second_pointer == NULL) return NULL; // Otherwise length stores actual length // of loop. // If needed, we can also print length of // loop. // printf("Length of loop is %d\n", length); // Now set first_pointer to the beginning // and second_pointer to beginning plus // cycle length which is length. first_pointer = second_pointer = head; while (length > 0) { second_pointer = second_pointer->next; --length; } // Now move both pointers at same speed so // that they meet at the beginning of loop. while (second_pointer != first_pointer) { second_pointer = second_pointer->next; first_pointer = first_pointer->next; } // If needed, we can also print length of // loop. // printf("Length of loop is %d", length); return first_pointer; } struct Node* newNode( int key) { struct Node* temp = ( struct Node*) malloc ( sizeof ( struct Node)); temp->data = key; temp->next = NULL; return temp; } // Driver program to test above function int main() { struct Node* head = newNode(50); head->next = newNode(20); head->next->next = newNode(15); head->next->next->next = newNode(4); head->next->next->next->next = newNode(10); // Create a loop for testing head->next->next->next->next->next = head->next->next; Node *res = detectCycle(head); if (res == NULL) cout << "No loop" ; else cout << "Loop is present at " << res->data; return 0; } // this code is contributed by shivanisinghss2110 |
C
// CPP program to implement Brent's cycle // detection algorithm to detect cycle in // a linked list. #include <stdio.h> #include <stdlib.h> /* Link list node */ struct Node { int data; struct Node* next; }; /* This function detects loop in the list If loop was there in the list then it returns, the first node of loop otherwise returns NULL */ struct Node* detectCycle( struct Node* head) { // if head is null then no loop if (head == NULL) return NULL; struct Node* first_pointer = head; struct Node* second_pointer = head->next; int power = 1; int length = 1; // This loop runs till we find the loop. // If there is no loop then second_pointer // ends at NULL . while (second_pointer != NULL && second_pointer != first_pointer) { // condition after which we will // update the power and length as // smallest power of two gives the // start of cycle. if (length == power) { // updating the power. power *= 2; // updating the length length = 0; first_pointer = second_pointer; } second_pointer = second_pointer->next; ++length; } // if it is null then no loop if (second_pointer == NULL) return NULL; // Otherwise length stores actual length // of loop. // If needed, we can also print length of // loop. // printf("Length of loop is %d\n", length); // Now set first_pointer to the beginning // and second_pointer to beginning plus // cycle length which is length. first_pointer = second_pointer = head; while (length > 0) { second_pointer = second_pointer->next; --length; } // Now move both pointers at same speed so // that they meet at the beginning of loop. while (second_pointer != first_pointer) { second_pointer = second_pointer->next; first_pointer = first_pointer->next; } // If needed, we can also print length of // loop. // printf("Length of loop is %d", length); return first_pointer; } struct Node* newNode( int key) { struct Node* temp = ( struct Node*) malloc ( sizeof ( struct Node)); temp->data = key; temp->next = NULL; return temp; } // Driver program to test above function int main() { struct Node* head = newNode(50); head->next = newNode(20); head->next->next = newNode(15); head->next->next->next = newNode(4); head->next->next->next->next = newNode(10); // Create a loop for testing head->next->next->next->next->next = head->next->next; Node *res = detectCycle(head); if (res == NULL) printf ( "No loop" ); else printf ( "Loop is present at %d" , res->data); return 0; } |
Java
// Java program to implement Brent's cycle // detection algorithm to detect cycle in // a linked list. class GFG { /* Link list node */ static class Node { int data; Node next; } /* This function detects loop in the list If loop was there in the list then it returns, the first node of loop otherwise returns null */ static Node detectCycle(Node head) { // if head is null then no loop if (head == null ) return null ; Node first_pointer = head; Node second_pointer = head.next; int power = 1 ; int length = 1 ; // This loop runs till we find the loop. // If there is no loop then second_pointer // ends at null . while (second_pointer != null && second_pointer != first_pointer) { // condition after which we will // update the power and length as // smallest power of two gives the // start of cycle. if (length == power) { // updating the power. power *= 2 ; // updating the length length = 0 ; first_pointer = second_pointer; } second_pointer = second_pointer.next; ++length; } // if it is null then no loop if (second_pointer == null ) return null ; // Otherwise length stores actual length // of loop. // If needed, we can also print length of // loop. // printf("Length of loop is %d\n", length); // Now set first_pointer to the beginning // and second_pointer to beginning plus // cycle length which is length. first_pointer = second_pointer = head; while (length > 0 ) { second_pointer = second_pointer.next; --length; } // Now move both pointers at same speed so // that they meet at the beginning of loop. while (second_pointer != first_pointer) { second_pointer = second_pointer.next; first_pointer = first_pointer.next; } // If needed, we can also print length of // loop. // printf("Length of loop is %d", length); return first_pointer; } static Node newNode( int key) { Node temp = new Node(); temp.data = key; temp.next = null ; return temp; } // Driver program to test above function public static void main(String[] args) { Node head = newNode( 50 ); head.next = newNode( 20 ); head.next.next = newNode( 15 ); head.next.next.next = newNode( 4 ); head.next.next.next.next = newNode( 10 ); // Create a loop for testing head.next.next.next.next.next = head.next.next; Node res = detectCycle(head); if (res == null ) System.out.println( "No loop" ); else System.out.println( "Loop is present at " + res.data); } } // This code is contributed by Lovely Jain |
Python3
# Python program to implement # Brent's cycle detection # algorithm to detect cycle # in a linked list. # Node class class Node: # Constructor to initialize # the node object def __init__( self , data): self .data = data self . next = None class LinkedList: # Function to initialize head def __init__( self ): self .head = None # Function to insert a new Node # at the beginning def push( self , new_data): new_node = Node(new_data) new_node. next = self .head self .head = new_node # Utility function to print # the linked LinkedList def printList( self ): temp = self .head while (temp): print (temp.data,end = " " ) temp = temp. next def detectCycle( self ): # if head is null # then no loop temp = self .head if not (temp): return False first_p = temp second_p = temp. next power = 1 length = 1 # This loop runs till we # find the loop. If there # is no loop then second # pointer ends at NULL while (second_p and second_p! = first_p): # condition after which # we will update the power # and length as smallest # power of two gives # the start of cycle. if (length = = power): # updating the power. power * = 2 # updating the length length = 0 first_p = second_p second_p = second_p. next length = length + 1 # if it is null then no loop if not (second_p) : return # Otherwise length stores # actual length of loop. # If needed, we can also # print length of loop. # print("Length of loop is ") # print (length) # Now set first_pointer # to the beginning and # second_pointer to # beginning plus cycle # length which is length. first_p = second_p = self .head while (length > 0 ): second_p = second_p. next length = length - 1 # Now move both pointers # at same speed so that # they meet at the # beginning of loop. while (second_p! = first_p) : second_p = second_p. next first_p = first_p. next return first_p # Driver program for testing llist = LinkedList() llist.push( 50 ) llist.push( 20 ) llist.push( 15 ) llist.push( 4 ) llist.push( 10 ) # Create a loop for testing llist.head. next . next . next . next . next = llist.head. next . next ; res = llist.detectCycle() if ( res.data): print ( "loop found at " ,end = ' ' ) print (res.data) else : print ( "No Loop " ) # This code is contributed by Gitanjali |
C#
/* C# program to implement Brent's cycle detection algorithm to detect cycle in a linked list */ using System; using System.Collections.Generic; namespace gfg { /* Link list node */ public class Node { public int Data; public Node Next; } class GFG { /* This function detects loop in the list If loop was there in the list then it returns, the first node of loop otherwise returns null */ public static Node DetectCycle(Node head) { // if head is null then no loop if (head == null ) return null ; Node first_pointer = head; Node second_pointer = head.Next; int power = 1; int length = 1; // This loop runs till we find the loop. // If there is no loop then second_pointer // ends at null . while (second_pointer != null && second_pointer != first_pointer) { // condition after which we will // update the power and length as // smallest power of two gives the // start of cycle. if (length == power) { // updating the power. power *= 2; // updating the length length = 0; first_pointer = second_pointer; } second_pointer = second_pointer.Next; ++length; } // if it is null then no loop if (second_pointer == null ) return null ; // Otherwise length stores actual length // of loop. // If needed, we can also print length of // loop. // Console.WriteLine("Length of loop is {0}", // length); // Now set first_pointer to the beginning // and second_pointer to beginning plus // cycle length which is length. first_pointer = second_pointer = head; while (length > 0) { second_pointer = second_pointer.Next; --length; } // Now move both pointers at same speed so // that they meet at the beginning of loop. while (second_pointer != first_pointer) { second_pointer = second_pointer.Next; first_pointer = first_pointer.Next; } // If needed, we can also print length of // loop. // Console.WriteLine("Length of loop is {0}", // length); return first_pointer; } static Node NewNode( int key) { Node temp = new Node(); temp.Data = key; temp.Next = null ; return temp; } // Driver program to test above function public static void Main() { Node head = NewNode(50); head.Next = NewNode(20); head.Next.Next = NewNode(15); head.Next.Next.Next = NewNode(4); head.Next.Next.Next.Next = NewNode(10); // Create a loop for testing head.Next.Next.Next.Next.Next = head.Next.Next; Node res = DetectCycle(head); if (res == null ) Console.WriteLine( "No loop" ); else Console.WriteLine( "Loop is present at " + res.Data); } } } |
Javascript
// JavaScript program to implement Brent's cycle detection // algorithm to detect cycle in a linked list. // Node class class Node { // Constructor to initialize the node object constructor(data) { this .data = data; this .next = null ; } } class LinkedList { // Function to initialize head constructor() { this .head = null ; } // Function to insert a new Node at the beginning push(new_data) { let new_node = new Node(new_data); new_node.next = this .head; this .head = new_node; } // Utility function to print the linked LinkedList printList() { let temp = this .head; while (temp) { console.log(temp.data, end= " " ); temp = temp.next; } } detectCycle() { // if head is null then no loop let temp = this .head; if (!temp) { return false ; } let first_p = temp; let second_p = temp.next; let power = 1; let length = 1; // This loop runs till we find the loop. If there is no loop then second pointer ends at NULL while (second_p && second_p != first_p) { // condition after which we will update the power and length as smallest power of two gives the start of cycle. if (length == power) { // updating the power. power *= 2; // updating the length length = 0; first_p = second_p; } second_p = second_p.next; length += 1; } // if it is null then no loop if (!second_p) { return ; } // Otherwise length stores actual length of loop. // If needed, we can also print length of loop. // console.log("Length of loop is "); // console.log(length); // Now set first_pointer to the beginning and // second_pointer to beginning plus cycle length which is length. first_p = second_p = this .head; while (length > 0) { second_p = second_p.next; length--; } // Now move both pointers at same speed so that they meet at the beginning of loop. while (second_p != first_p) { second_p = second_p.next; first_p = first_p.next; } return first_p; } } // Driver program for testing let llist = new LinkedList(); llist.push(50); llist.push(20); llist.push(15); llist.push(4); llist.push(10); // Create a loop for testing llist.head.next.next.next.next.next = llist.head.next.next; let res = llist.detectCycle(); if (res) { console.log( "loop found at " , res.data); } else { console.log( "No Loop" ); } |
Output:
Loop is present at 15
Time Complexity: O(m + n) where m is the smallest index of the sequence which is the beginning of a cycle, and n is the cycle’s length.
Auxiliary Space : – O(1)
References :
https://en.wikipedia.org/wiki/Cycle_detection#Brent’s_algorithm
github
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!