LeetCode 1971 Find if Path Exists - Problem Statement

The problem

Given n nodes (0 to n-1) and undirected edges, determine if a path exists between source and destination.

With n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2:

  • Edge 0-1, 1-2, 2-0. All nodes connected.
  • Path exists: 0 → 1 → 2 or 0 → 2 directly.
  • Return true.

With n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5:

  • Nodes 0,1,2 form one component. Nodes 3,4,5 form another.
  • No path from 0 to 5. Return false.

Constraints: 1n2×1051 \le n \le 2 \times 10^5.