-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidPath
More file actions
31 lines (30 loc) · 786 Bytes
/
validPath
File metadata and controls
31 lines (30 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public boolean validPath(int n, int[][] edges, int source, int destination) {
List<List<Integer>> adj = new ArrayList<>();
Queue<Integer> q = new LinkedList<>();
boolean[] vis = new boolean[n+1];
if(source==destination) return true;
for(int i=0;i<n;i++) adj.add(new ArrayList<>());
for(int[] node:edges)
{
adj.get(node[0]).add(node[1]);
adj.get(node[1]).add(node[0]);
}
q.add(source);
while(!q.isEmpty())
{
Integer node = q.poll();
vis[node]=true;
if(node==destination) return true;
for(Integer temp:adj.get(node))
{
if(vis[temp]!=true)
{
q.add(temp);
vis[temp]=true;
}
}
}
return false;
}
}