Create the adj list and then do DFS from the source node. If the path exists, it will eventually show up in the stack.
from collections import defaultdict
classSolution:defvalidPath(self, n:int, edges: List[List[int]], source:int, destination:int)->bool:# create adj list then dfs adj = defaultdict(list)for u, v in edges: adj[u].append(v) adj[v].append(u) marked =set() stack =[source] marked.add(source)while stack: v = stack.pop()if v == destination:returnTruefor w in adj[v]:if w notin marked: stack.append(w) marked.add(w)returnFalse