Find length of Loop | Practice | GeeksforGeeks

Given a linked list of size N. The task is to complete the function countNodesinLoop() that checks whether a given Linked List contains a loop or not and if the loop is present then return the count of nodes in a loop or else return 0. C is the position of the node to which the last node is connected. If it is 0 then no loop.
 
Example 1:
Input:
N = 10
value[]={25,14,19,33,10,21,39,90,58,45}
C = 4
Output: 7
Explanation: The loop is 45->33. So
length of loop is 33->10->21->39->
90->58->45 = 7. The number 33 is
connected to the last node to form the
loop because according to the input the
4th node from the beginning(1 based
index) will be connected to the last
node for the loop.

Example 2:
Input:
N = 2
value[] = {1,0}
C = 1
Output: 2
Explanation: The length of the loop
is 2.
Your Task:
The task is to complete the function countNodesinLoop() which contains the only argument as reference to head of linked list and return the lenght of the loop ( 0 if there is no loop).
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 500
0 <= C <= N-1
0
awektoppo5 days ago
int countNodesinLoop(struct Node *head){   // Floyd Loop finding method with counting loop elements
   Node *slow=head;   Node *fast = NULL;   if(head->next)       fast = head->next;   int cnt=0;   bool start=false;   while(fast){       if(slow == fast){           if(start)   break;           start = !start;       }       if(start)   cnt++;       if(slow->next)  slow = slow->next;       else break;       if(fast->next) fast=fast->next;       else break;       if(fast->next) fast=fast->next;       else break;   }   return cnt;         }
0
tthakare732 weeks ago
//Java Solution  TC -> 1.55
//hint -> tortoise and hare algorithm
class Solution{
    static int findcycleStart(Node root){
        Node fast = root, slow = root;
        boolean loop = false;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast) {
                loop = true;
                break;
            }
        }
        if(!loop) return 0;
        int count = 1;
        slow = slow.next;
        while(fast != slow){
            slow = slow.next;
            count++;
        }
        return count;
    }
    static int countNodesinLoop(Node head){
        if(head == null) return 0;
        return findcycleStart(head);
    }
}

0
hharshit81183 weeks ago
T(N) : O(N)
int countNodesinLoop(struct Node *head){   if(head == NULL){       return 0;   }   bool isloop = false;   int len = 0;   Node *slow = head, *fast = head;   while(fast && fast->next){       slow = slow->next;       fast = fast->next->next;       if(slow == fast){           isloop = true;           break;       }   }   if(isloop){       slow = head;       while(slow != fast){           slow = slow->next;           fast = fast->next;       }       do{         len++;         fast = fast->next;       }while(fast != slow);   }   return len;}
0
roysidharth5823 weeks ago
static int countNodesinLoop(Node head)
   {
       Node fast=head;
       Node slow=head;
       while(fast!=null && fast.next!=null){
           slow=slow.next;
           fast=fast.next.next;
           
           
           if(slow==fast){
               int c=1;
               slow=slow.next;
               while(slow!=fast){
                   c++;
                   slow=slow.next;
               }
               return c;
           }
       } return 0;
   }
0
jpragati053 weeks ago
JAVA SOLUTION USING HASHSET
class Solution
{
    //Function to find the length of a loop in the linked list.
    static int count(Node head){
        int res=1;
        Node temp=head;
        while(temp.next!=head){
            res++;
            temp=temp.next;
        }
        return res;
    }
    static int countNodesinLoop(Node head)
    {
        //Add your code here.
        Set<Node>hs=new HashSet<>();
        while(head!=null){
            if(hs.contains(head))
            return count(head);
            hs.add(head);
            head=head.next;
        }
        return 0;
    }
}
0
amiransarimy4 weeks ago
Python Solutions
 
def countNodesinLoop(head):
   count = 0
   fast, slow = head, head
   while fast and fast.next:
       slow = slow.next
       fast = fast.next.next
       
       if fast == slow:
           count += 1
           slow = slow.next
           
           while fast != slow:
               count += 1
               slow = slow.next
           
           return count
   return 0
0
rmn51241 month ago
int countNodesinLoop(struct Node *head)
{
     Node*slow=head,*fast=head;

        while(fast and fast->next){
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast){  // loop detected
                int c=1;
                slow=slow->next;
                while(fast!=slow){ //check the length
                    c++;
                    slow=slow->next;
                }
                return c;
            }
        }
        return 0;
    }

0
roopsaisurampudi1 month ago
static int countNodesinLoop(Node head)
    {
        //Add your code here.
        Node temp = head;
        Node slow = head;
        Node fast = head;
        Node intersection = null;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (fast == slow) {
                intersection = slow;
                break;
            }
        }
        if (fast == null || fast.next == null) return 0;
        Node start = intersection.next;
        int len = 1;
        while(intersection != start) {
            len++;
            start = start.next;
        }
        return len;
    }
0
sgupta0781 month ago
C++ solution
int countNodesinLoop(struct Node *head)
{
    // Code here
    
    
    Node* start=NULL;
    Node* slow = head;
    Node* fast = head;
    
    
    // find the loop start
    while(slow!=NULL && fast!=NULL && fast->next!=NULL){
        slow=slow->next;
        fast=fast->next->next;
        if(slow==fast){
            start=slow;
            break;
        }
    }
    
    // count nodes between loop
    Node* check=start;
    int count=0;
    while(start!=NULL){
        count++;
        start=start->next;
        if(start==check){
            break;
        }
    }
    return count;
    
}
0
pritamtech19981 month ago
int count(Node *t1, Node *t2){   int len = 0;   while((t2 = t2->next) and t1 != t2 and (len = len+1));   return len+1;}
int countNodesinLoop(struct Node *head){   Node *t1 = head, *t2 = head;   while(t1 and t2 and t1->next and t2->next and t2->next->next and (t1 = t1->next) and (t2 = t2->next->next)){       if(t1 == t2)           return count(t1, t2);   }   return 0;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.