Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/graph/clone_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, val=0, neighbors=None):


class Solution:
def cloneGraph(self, node):
def cloneGraph(self, source):
"""
Clone an undirected graph.

Expand All @@ -40,8 +40,25 @@ def cloneGraph(self, node):
Time Complexity: O(N + E) where N is number of nodes and E is edges
Space Complexity: O(N) for the hashmap
"""
# TODO: Implement solution
pass
if not source:
return

Comment on lines +43 to +45
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an empty input graph, this returns None (via bare return). The docstring currently says the method returns Node - cloned graph; please clarify in the Returns: section that None is returned when source is None (or explicitly return None for clarity).

Copilot uses AI. Check for mistakes.
visited = {}

def dfs(node):
if node.val in visited:
return visited[node.val]

new_node = Node(node.val)
new_neighbors = []
visited[node.val] = new_node
for neighbor in node.neighbors:
new_neighbors.append(dfs(neighbor))
new_node.neighbors = new_neighbors

return new_node

return dfs(source)


# Example usage (for testing locally)
Expand Down