Skip to content

Commit

Permalink
Merge pull request #139 from Samruddhi345A/main
Browse files Browse the repository at this point in the history
GFg POTD 09-10-2024 in cpp
  • Loading branch information
Gyanthakur authored Oct 10, 2024
2 parents edcdfa8 + 88d58c5 commit ffd0c0b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions october_2024/potd_09_10_2024.cpp
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];


}
};

0 comments on commit ffd0c0b

Please sign in to comment.