Skip to content

Commit

Permalink
Create: 133-Clone-Graph.c
Browse files Browse the repository at this point in the history
  • Loading branch information
th-blitz committed Sep 30, 2022
1 parent 78634a9 commit 5afdafe
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions c/133-Clone-Graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/**
* Definition for a Node.
* struct Node {
* int val;
* int numNeighbors;
* struct Node** neighbors;
* };
*/

struct Node* create_node(int val, int numNeighbors) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node -> val = val;
node -> numNeighbors = numNeighbors;
if (numNeighbors > 0) {
node -> neighbors = (struct Node**)malloc(numNeighbors * sizeof(struct Node*));
} else {
node -> neighbors = NULL;
}
return node;
}

struct Node* clone_node(struct Node* s, struct Node** hashset) {

if (s == NULL) return s;
if (hashset[(s -> val) - 1] != NULL) return hashset[(s -> val) - 1];

struct Node* new_node = create_node(s -> val, s -> numNeighbors);
hashset[(s -> val) - 1] = new_node;
for (int i = 0; i < s -> numNeighbors; i++) {
new_node -> neighbors[i] = clone_node(s -> neighbors[i], hashset);
}
return new_node;
}

struct Node *cloneGraph(struct Node *s) {

struct Node** hashset = (struct Node**)malloc(100 * sizeof(struct Node*));
for (int i = 0; i < 100; i++) {
hashset[i] = NULL;
}
struct Node* s_clone = clone_node(s, hashset);
free(hashset);
return s_clone;
}

0 comments on commit 5afdafe

Please sign in to comment.