MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

GeeksForGeeks/Detect Loop in Linked List/Solution.java:13–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12
13class Solution {
14 public static boolean detectLoop(Node head){
15 // Add code here
16 //basic metehod we use here is we take two node fast and slow fast node will movw twice fast as slow
17 //during traversal of list we check that is value of slow and fast node are equal it means there is a loop.
18 //otherwise it is not possible
19 Node slow=head,fast=head;
20 boolean f=false;
21 while(fast!=null && fast.next!=null){
22 slow=slow.next;
23 fast=fast.next.next;
24 if(slow==fast){
25 f=true;
26 break;
27 }
28
29 }
30 return f;
31 }
32}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected