https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1
The code I wrote.....
class Solution {
// Function to detect cycle in an undirected graph.
public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
// Code here
boolean []visited = new boolean[V];
for(int i =0; i<adj.size();i++){
if(visited[i] == false){
if(iscycleUtil(adj,i, -1, visited)){
return true;
}
}
}
return false;
}
public boolean iscycleUtil( ArrayList<ArrayList<Integer>> adj, int current, int parent, boolean[] visited){
visited[current] = true;
for(int j =0; j<(adj.get(current)).size();j++){
for(int i : adj.get(current)){
if(visited[i] == false){
if(iscycleUtil(adj,i,current,visited)){
return true;
}
}
else if(visited[i] == true && i != parent){
return true;
}
}
}
return false;
}
}
My test cases are failings, I am not getting where I went wrong.