Given a 2D matrix, the task is to convert it into a doubly-linked list with four pointers that are next, previous, up, and down, each node of this list should be connected to its next, previous, up, and down nodes.
Input: 2D matrix 1 2 3 4 5 6 7 8 9 Output:
Approach: The main idea is to construct a new node for every element of the matrix and recursively create it’s up, down, previous and next nodes and change the pointer of previous and up pointers respectively.
Below is the implementation of the above approach:
1 2 3 4 5 6 7 8 9
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- Construct a linked list from 2D matrix
- Construct a linked list from 2D matrix (Iterative Approach)
- Construct Linked List from 3D Matrix
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Memory efficient doubly linked list
- Delete a node in a Doubly Linked List
- Convert a given Binary Tree to Doubly Linked List | Set 1
- QuickSort on Doubly Linked List
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Merge Sort for Doubly Linked List
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a Binary Tree into Doubly Linked List in spiral fashion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.