# Construct a linked list from 2D matrix

Given a matrix. Convert it into a linked list matrix such that each node is connected to its next right and down node.

Example:

```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
```

Question Source : Factset Interview Experience | Set 9

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to construct a new node for every element of matrix and recursively create its down and right nodes.

## C++

```// CPP program to construct a linked list
// from given 2D matrix
#include <bits/stdc++.h>
using namespace std;

// struct node of linked list
struct Node {
int data;
Node* right, *down;
};

// constructed from 2D matrix
Node* construct(int arr[][3], int i, int j,
int m, int n)
{
// return if i or j is out of bounds
if (i > n - 1 || j > m - 1)
return NULL;

// create a new node for current i and j
// and recursively allocate its down and
// right pointers
Node* temp = new Node();
temp->data = arr[i][j];
temp->right = construct(arr, i, j + 1, m, n);
temp->down  = construct(arr, i + 1, j, m, n);
return temp;
}

// utility function for displaying
{
// pointer to move right
Node* Rp;

// pointer to move down

// loop till node->down is not NULL
while (Dp) {
Rp = Dp;

// loop till node->right is not NULL
while (Rp) {
cout << Rp->data << " ";
Rp = Rp->right;
}
cout << "\n";
Dp = Dp->down;
}
}

// driver program
int main()
{
// 2D matrix
int arr[][3] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

int m = 3, n = 3;
Node* head = construct(arr, 0, 0, m, n);
return 0;
}
```

## Java

```// Java program to construct a linked list
// from given 2D matrix

static class Node {
int data;
Node right;
Node down;
};

// constructed from 2D matrix
static Node construct(int arr[][], int i, int j,
int m, int n) {

// return if i or j is out of bounds
if (i > n - 1 || j > m - 1)
return null;

// create a new node for current i and j
// and recursively allocate its down and
// right pointers
Node temp = new Node();
temp.data = arr[i][j];
temp.right = construct(arr, i, j + 1, m, n);
temp.down = construct(arr, i + 1, j, m, n);
return temp;
}

// utility function for displaying

// pointer to move right
Node Rp;

// pointer to move down

// loop till node->down is not NULL
while (Dp != null) {
Rp = Dp;

// loop till node->right is not NULL
while (Rp != null) {
System.out.print(Rp.data + " ");
Rp = Rp.right;
}
System.out.println();
Dp = Dp.down;
}
}

// driver program
public static void main(String args[]) {
// 2D matrix
int arr[][] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };

int m = 3, n = 3;
Node head = construct(arr, 0, 0, m, n);
}

}
// This code is contributed by Sumit Ghosh
```

Output:

```1 2 3
4 5 6
7 8 9
```

This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.4 Average Difficulty : 2.4/5.0
Based on 5 vote(s)