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)
- Reverse a Linked List in groups of given size (Iterative Approach)
- Iterative approach for removing middle points in a linked list of line segements
- Construct a linked list from 2D matrix
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- A Programmer's approach of looking at Array vs. Linked List
- Recursive approach for alternating split of Linked List
- Modify contents of Linked List - Recursive approach
- Recursive Approach to find nth node from the end in the linked list
- Iterative Merge Sort for Linked List
- Iterative selection sort for linked list
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Search an element in a Linked List (Iterative and Recursive)
- Find Length of a Linked List (Iterative and Recursive)
- Check if linked list is sorted (Iterative and Recursive)
- Print the alternate nodes of linked list (Iterative Method)
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.
Improved By : souravdutta123