-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from Samruddhi345A/main
GFg POTD 09-10-2024 in cpp
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*structure of the node of the linked list is as | ||
struct Node | ||
{ | ||
int data; | ||
Node* right, *down; | ||
Node(int x){ | ||
data = x; | ||
right = NULL; | ||
down = NULL; | ||
} | ||
}; | ||
*/ | ||
|
||
// function must return the pointer to the first element of the in linked list i.e. that | ||
// should be the element at arr[0][0] | ||
class Solution { | ||
public: | ||
Node* constructLinkedMatrix(vector<vector<int>>& mat) { | ||
if (mat.empty() || mat[0].empty()) { | ||
return nullptr; | ||
} | ||
|
||
int rows = mat.size(); | ||
int cols = mat[0].size(); | ||
|
||
Node*** node = new Node**[rows]; | ||
for (int i = 0; i < rows; i++) { | ||
node[i] = new Node*[cols]; | ||
} | ||
|
||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
node[i][j] = new Node(mat[i][j]); | ||
} | ||
} | ||
|
||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (j < cols - 1) { | ||
node[i][j]->right = node[i][j + 1]; | ||
} | ||
if (i < rows - 1) { | ||
node[i][j]->down = node[i + 1][j]; | ||
} | ||
} | ||
} | ||
|
||
return node[0][0]; | ||
|
||
|
||
} | ||
}; |