Skip to content
Veltoryx Technologies
ProProblem 115● MediumGeeksforGeeks / LeetCode #261 ↗3 min readUpdated 30 October 2026

Detect Cycle in an Undirected Graph

Detect cycles in an undirected graph by tracking parent pointers during DFS.

01The problem

Given an undirected graph with V vertices and E edges, represented as an adjacency list adj, determine if the graph contains any cycle.

text
0 --- 1
 \   /
   2

Output: true (triangle 0-1-2-0 forms a cycle)

Constraints

  • 1 <= V, E <= 10^5
  • The graph may be disconnected (consist of multiple disjoint components).

Loading the rest of the lesson…