Simple java BFS solution


#1
import java.util.*;
class MyLinkedList extends LinkedList<Integer> {
}
class Graph{
    int v;
    MyLinkedList[] lists;
    public Graph(int v){
        this.v=v;
        this.lists=new MyLinkedList[v];
        for(int i=0;i<v;i++){
            lists[i]=new MyLinkedList();
        }
    }
    void addEdge(int u, int v) {
		lists[u].add(v);
	}
	boolean isReachable(int s,int d){
	    LinkedList<Integer> temp;
	    boolean visited[] =new boolean[v];
	    LinkedList<Integer> q=new LinkedList<>();
	    visited[s]=true;
	    q.add(s);
	    Iterator<Integer> i;
	    while(q.size()!=0){
	        s=q.poll();
	        int n;
	        i=lists[s].listIterator();
	        while(i.hasNext()){
	            n=i.next();
	            if(n==d){
	               return true;
	            }
	            if(!visited[n]){
	                visited[n]=true;
	                q.add(n);
	            }
	        }
	    }
	    return false;
	}
}

public class Solution {
    public int solve(int A, int[][] B) {
        int V=A;
        Graph g=new Graph(V);
        for(int[] i:B){
            g.addEdge(i[0]-1,i[1]-1);
        }
        int u=0,v=A-1;
        if(g.isReachable(u,v)){
            return 1;
        }
        return 0;
    }
}