Linked List Matrix | Practice | GeeksforGeeks

Given a Matrix mat of N*N size, the task is to complete the function constructLinkedMatrix(), that constructs a 2D linked list representation of the given matrix.
Input : 2D matrix 
1 2 3
4 5 6
7 8 9

Output :
1 -> 2 -> 3 -> NULL
|    |    |
v    v    v
4 -> 5 -> 6 -> NULL
|    |    |
v    v    v
7 -> 8 -> 9 -> NULL
|    |    |
v    v    v
NULL NULL NULL
Input:
The fuction takes 2 argument as input, first the 2D matrix mat[][] and second an integer variable N, denoting the size of the matrix.
There will be T test cases and for each test case the function will be called separately.

Output:
The function must return the reference pointer to the head of the linked list.

Constraints:
1<=T<=100
1<=N<=150

Example:
Input:
2
3
1 2 3 4 5 6 7 8 9
2
1 2 3 4
Output:
1 2 3 4 5 6 7 8 9
1 2 3 4
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
satyamkumar96002 months ago
 static Node constructl(Node head,int data){
        if(head == null){
            Node temp = new Node(data);
            head = temp;
            return head;
        }
        Node temp = head;
        while(temp.right!=null){
            temp =temp.right;
        }
        Node t = new Node(data);
        temp.right = t;
        return head;
    }
    static Node construct(int arr[][],int n)
    {
        //Add your code here.
        Node arrl[] = new Node[n];
        for (int i = 0;i<n;i++){
            
                   for(int j =0;j<n;j++){
                
                  arrl[i] =  constructl(arrl[i],arr[i][j]);    
                }
                
        } 
        
        for(int i = 0;i<n-1;i++){
            Node head = arrl[i];
            Node anhead = arrl[i+1];
            while(head!= null){
                head.down = anhead;
                head = head.right;
                anhead = anhead.right;
            }
        }
        
        return arrl[0];
        
    }
+2
haritasboy12052 months ago
C++ | Solution
Node* constructLinkedMatrix(int mat[MAX][MAX], int n)
{
    // code here
    Node* head = new Node(mat[0][0]);
    Node* temp = head;
    
    for(int i=0;i<n;i++)
    {
        Node* temp2 = temp;
        for(int j=0;j<n;j++)
        {
            if(j+1 == n)
                temp->right = NULL;
            else
                temp->right = new Node(mat[i][j+1]);
            if(i+1 == n)
                temp->down = NULL;
            else
                temp->down = new Node(mat[i+1][j]);
            temp = temp->right;    
        }
        temp = temp2;
        temp = temp->down;
    }
    return head;
}
+1
mohankrishnapeddineni3 months ago
//JAVA CODE
 static Node construct(int arr[][],int n)   {       Node head=new Node(arr[0][0]);       Node temp=head;       for(int i=0;i<n;i++){           Node t = temp;         for(int j=0;j<n;j++){             if(j+1==n){              temp.right =null;             }             else temp.right = new Node(arr[i][j+1]);             if(i+1==n){                temp.down = null;                              }             else temp.down = new Node(arr[i+1][j]);             temp =temp.right;                      }         temp = t;         temp =temp.down;       }       return  head;   }
0
user_4tly3 months ago
Node* constructLinkedMatrix(int mat[MAX][MAX], int n){   // code here   Node* head = NULL;   Node* lastRowHead = NULL;   Node* lastRowPointer = NULL;      for(int i=0 ; i<n ; i++) {       Node* temp = NULL;       for(int j=0 ; j<n ; j++) {           Node* nw = new Node(mat[i][j]);           if(temp != NULL){               temp->right = nw;           }           if(i != 0){               lastRowPointer->down = nw;               lastRowPointer = lastRowPointer->right;           }           temp = nw;           if(j==0){               lastRowHead = temp;           }           if(head == NULL)               head = temp;       }       lastRowPointer = lastRowHead;   }   return head;} 
0
yadavkapil23363 months ago
C++
Node* constructLinkedMatrix(int mat[MAX][MAX], int n){   int i,j;   Node* head=new Node(mat[0][0]);       Node* temp1=head;       Node* temp2=head;       for(i=1;i<n;i++)       {           temp1->right=new Node(mat[0][i]);           temp1=temp1->right;       }
   for(j=1;j<n;j++)   {       temp1=head;       temp2=head;       while(temp1->down!=NULL)       temp1=temp1->down;              while(temp2->down!=NULL)       temp2=temp2->down;              temp2->down=new Node(mat[j][0]);       temp2=temp2->down;              for(i=1;i<n;i++)       {           temp1=temp1->right;           temp2->right=new Node(mat[j][i]);           temp1->down=temp2->right;           temp2=temp2->right;       }   }   return(head);                   }
0
abhisinha6563 months ago
Node* newNode(int d){   Node* temp = (struct Node*)malloc(sizeof(struct Node));   temp->data = d;   temp->right = temp->down = NULL;   return temp;}Node* constructLinkedMatrix(int mat[MAX][MAX], int n){   // code here   Node* mainhead = NULL;
   Node* head[n];   Node *righttemp, *newptr;
   for (int i = 0; i < n; i++) {       head[i] = NULL;       for (int j = 0; j < n; j++) {           newptr = newNode(mat[i][j]);
           if (!mainhead)               mainhead = newptr;           if (!head[i])               head[i] = newptr;           else               righttemp->right = newptr;           righttemp = newptr;       }   }   for (int i = 0; i < n - 1; i++) {       Node *temp1 = head[i], *temp2 = head[i + 1];       while (temp1 && temp2) {           temp1->down = temp2;           temp1 = temp1->right;           temp2 = temp2->right;       }   }   return mainhead;} 
0
ankitsinha1112005 months ago
Node* constructLinkedMatrix(int mat[MAX][MAX], int n)
{
        Node* temp2=NULL,*temp3,*temp4;
    for(int i=n-1;i>=0;i--)
    {
        Node* temp1=NULL;
        for(int j=n-1;j>=0;j--)
        {
            Node* temp=new Node(mat[i][j]);
            temp->right=temp1;
            if(i==n-1)
            temp->down=temp2;
            if(j==0)
                temp3=temp;
            temp1=temp;
        }
        if(i!=n-1)
        {
            temp4=temp3;
            while(temp4!=NULL)
            {
                temp4->down=temp2;
                temp4=temp4->right;
                temp2=temp2->right;
            }
        }
        temp2=temp3;
    }
    return temp3;
}

0
ankitsinha1112005 months ago
 
+1
imranwahid6 months ago
Easy C++ solution
0
sushilkumarskp6187 months ago
What is wrong with this, error- null pointer error in bold-line below. 
 
 static Node construct(int arr[][],int n)   {       Node headnode = new Node(arr[0][0]);        Node temp1 = headnode;              for(int i = 0; i<n; i++){            Node temp =temp1;                       for(int j=0; j<n;j++){                              temp.data = arr[i][j];               temp = temp.right;           }           temp1 = temp1.down;       }       return headnode;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.