Detect Loop in linked list | Practice | GeeksforGeeks

Given a linked list of N nodes. The task is to check if the linked list has a loop. Linked list can contain self loop.
Example 1:
Input:
N = 3
value[] = {1,3,4}
x = 2
Output: True
Explanation: In above test case N = 3.
The linked list with nodes N = 3 is
given. Then value of x=2 is given which
means last node is connected with xth
node of linked list. Therefore, there
exists a loop.
Example 2:
Input:
N = 4
value[] = {1,8,3,4}
x = 0
Output: False
Explanation: For N = 4 ,x = 0 means
then lastNode->next = NULL, then
the Linked list does not contains
any loop.
Your Task:
The task is to complete the function detectloop() which contains reference to the head as only argument. This function should return true if linked list contains loop, else return false.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 104
1 ≤ Data on Node ≤ 103
0
laxmankohar714 hours ago
                        Java Solution Using Two Pointer Technique
 
 public static boolean detectLoop(Node head){       Node slow_ptr = head;       Node fast_ptr = head;              while(slow_ptr != null && fast_ptr != null && fast_ptr.next != null){           slow_ptr = slow_ptr.next;           fast_ptr = fast_ptr.next.next;                      if(slow_ptr == fast_ptr){               return true;           }       }       return false;   }
0
yuvrajranabtcse201 day ago
c++ ans...time o(n) space o(1)...
   Node *dub=head->next;;       Node *sing=head;       while((sing!=NULL)&&(dub!=NULL)){           if(dub==sing)return true;           dub=dub->next;           if(dub!=NULL)dub=dub->next;           sing=sing->next;       }       return false;   }
0
hardik200404
This comment was deleted.
+2
pankaj0064 days ago
what sort of explanation is given, don't understand this x concept. Not mentioned anywhere
0
thatspykid5 days ago
Simple C++ Solution using two poniters method:
 bool detectLoop(Node* head)   {       // your code here       Node* slow = head;       Node* fast = head;              while(slow && fast && fast->next)       {           slow = slow->next;           fast = fast->next->next;           if(slow == fast)           {               return true;           }       }       return false;   }
 
// @ that_spy_kid
0
ksbsbisht1376 days ago
    bool detectLoop(Node* head) {       //False = No Cycle and True= Cycle       if(head==NULL) return false;       Node* tor=head;       Node* rab=head->next;       if(rab==NULL) return false;       while(rab!=NULL) {           if(tor==rab) return true;           tor=tor->next;           rab=rab->next;           if(rab!=NULL)               rab=rab->next;           else break;       } return false;   }
-1
sureshbscsses211 week ago
java solution 
 
public class Solution {public boolean hasCycle(ListNode head) {ListNode s=head;if(s==null){return false;}ListNode f=head.next;while(s!=null){if(s==null || f==null){return false;}if(s==f){return true;}s=s.next;if(s==null || f.next==null){return false;}f=f.next.next;}return false;}}
0
akaashhardha1 week ago
 bool detectLoop(Node* head)   {       Node *fast = head;       Node *slow = head;       while(fast && fast->next){           slow = slow->next;           fast = fast->next->next;           if(slow == fast)               return true;       }       return false;   }
0
harshscode2 weeks ago
  if(!head)       return false;           Node *slow=head;      Node *fast=head;      while(fast and fast->next)      {          slow=slow->next;          fast=fast->next->next;                  if(slow==fast)          return true;      }      return false;
-1
kumharashvin2042 weeks ago
{   public:   //Function to check if the linked list has a loop.   bool detectLoop(Node* head)   {      Node* slow = head;      Node* fast = head;            while(fast && fast->next){          slow = slow->next;          fast=fast->next->next;          if(slow==fast){              return true;          }      }      return false;   }}; 
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.