Given a matrix, the task is to construct a linked list matrix in which each node is connected to its right and down node.
Input: [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
A recursive solution for this problem has been already discussed in this post. Below is an iterative approach for the problem:
- The idea is to create m linked lists (m = number of rows) whose each node stores its right node. The head pointers of each m linked lists are stored in an array of nodes.
- Then, traverse m lists, for every ith and (i+1)th list, set the down pointers of each node of ith list to its corresponding node of (i+1)th list.
Below is the implementation of the above approach:
1 2 3 4 5 6 7 8 9
Time complexity: O(M * N)
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.
- Iterative approach for removing middle points in a linked list of line segements
- Reverse a Linked List in groups of given size (Iterative Approach)
- Construct a Doubly linked linked list from 2D Matrix
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Construct a linked list from 2D matrix
- Construct Linked List from 3D Matrix
- 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
- Find Length of a Linked List (Iterative and Recursive)
- Search an element in a Linked List (Iterative and Recursive)
- Print the alternate nodes of linked list (Iterative Method)
- Check if linked list is sorted (Iterative and Recursive)
- Iterative Merge Sort for Linked List
- Print the last k nodes of the linked list in reverse order | Iterative Approaches
- Iterative selection sort for linked list
- Reverse alternate K nodes in a Singly Linked List - Iterative Solution
- A Programmer's approach of looking at Array vs. Linked List
- Recursive approach for alternating split of Linked List
- Recursive Approach to find nth node from the end in the linked list
- Modify contents of Linked List - Recursive approach
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.