C Program For Flattening A Multilevel Linked List
Last Updated :
14 Feb, 2023
Given a linked list where in addition to the next pointer, each node has a child pointer, which may or may not point to a separate list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in below figure.You are given the head of the first level of the list. Flatten the list so that all the nodes appear in a single-level linked list. You need to flatten the list in way that all nodes at first level should come first, then nodes of second level, and so on.
Each node is a C struct with the following definition.
C
struct List
{
int data;
struct List *next;
struct List *child;
};

The above list should be converted to 10->5->12->7->11->4->20->13->17->6->2->16->9->8->3->19->15
The problem clearly says that we need to flatten level by level. The idea of a solution is, we start from the first level, process all nodes one by one, if a node has a child, then we append the child at the end of the list, otherwise, we don't do anything. After the first level is processed, all next-level nodes will be appended after the first level. The same process is followed for the appended nodes.
1) Take "cur" pointer, which will point to head of the first level of the list
2) Take "tail" pointer, which will point to end of the first level of the list
3) Repeat the below procedure while "curr" is not NULL.
I) if current node has a child then
a) append this new child list to the "tail"
tail->next = cur->child
b) find the last node of new child list and update "tail"
tmp = cur->child;
while (tmp->next != NULL)
tmp = tmp->next;
tail = tmp;
II) move to the next node. i.e. cur = cur->next
Following is the implementation of the above algorithm.
C
// Program to flatten list with next
// and child pointers
#include <stdio.h>
#include <stdlib.h>
// Macro to find number of elements
// in array
#define SIZE(arr) (sizeof(arr)/
sizeof(arr[0]))
// A linked list node has data,
// next pointer and child pointer
struct Node
{
int data;
struct Node *next;
struct Node *child;
};
// A utility function to create a linked list
// with n nodes. The data of nodes is taken
// from arr[]. All child pointers are set as NULL
struct Node *createList(int *arr, int n)
{
struct Node *head = NULL;
struct Node *p;
int i;
for (i = 0; i < n; ++i)
{
if (head == NULL)
head = p = (struct Node *)malloc(sizeof(*p));
else
{
p->next = (struct Node *)malloc(sizeof(*p));
p = p->next;
}
p->data = arr[i];
p->next = p->child = NULL;
}
return head;
}
// A utility function to print all nodes
// of a linked list
void printList(struct Node *head)
{
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("");
}
// This function creates the input list.
// The created list is same as shown in
// the above figure
struct Node *createList(void)
{
int arr1[] = {10, 5, 12, 7, 11};
int arr2[] = {4, 20, 13};
int arr3[] = {17, 6};
int arr4[] = {9, 8};
int arr5[] = {19, 15};
int arr6[] = {2};
int arr7[] = {16};
int arr8[] = {3};
// Create 8 linked lists
struct Node *head1 = createList(arr1,
SIZE(arr1));
struct Node *head2 = createList(arr2,
SIZE(arr2));
struct Node *head3 = createList(arr3,
SIZE(arr3));
struct Node *head4 = createList(arr4,
SIZE(arr4));
struct Node *head5 = createList(arr5,
SIZE(arr5));
struct Node *head6 = createList(arr6,
SIZE(arr6));
struct Node *head7 = createList(arr7,
SIZE(arr7));
struct Node *head8 = createList(arr8,
SIZE(arr8));
// Modify child pointers to create the
// list shown above
head1->child = head2;
head1->next->next->next->child = head3;
head3->child = head4;
head4->child = head5;
head2->next->child = head6;
head2->next->next->child = head7;
head7->child = head8;
/* Return head pointer of first linked list.
Note that all nodes are reachable from
head1 */
return head1;
}
/* The main function that flattens a
multilevel linked list */
void flattenList(struct Node *head)
{
// Base case
if (head == NULL)
return;
struct Node *tmp;
/* Find tail node of first level
linked list */
struct Node *tail = head;
while (tail->next != NULL)
tail = tail->next;
// One by one traverse through all nodes
// of first level linked list till we
// reach the tail node
struct Node *cur = head;
while (cur != tail)
{
// If current node has a child
if (cur->child)
{
// then append the child at the
// end of current list
tail->next = cur->child;
// and update the tail to new
// last node
tmp = cur->child;
while (tmp->next)
tmp = tmp->next;
tail = tmp;
}
// Change current node
cur = cur->next;
}
}
// Driver code
int main(void)
{
struct Node *head = NULL;
head = createList();
flattenList(head);
printList(head);
return 0;
}
Output:
10 5 12 7 11 4 20 13 17 6 2 16 9 8 3 19 15
Time Complexity: Since every node is visited at most twice, the time complexity is O(n) where n is the number of nodes in given linked list.
Space complexity: O(n),where n is the number of nodes in the linked list. This is because the code dynamically allocates memory for each node in the linked list and does not have any additional data structures that would cause an increase in space usage.
Please refer complete article on Flatten a multilevel linked list for more details!
Similar Reads
C Program For Deleting A Node In A Linked List
We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from
3 min read
C Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1->3->1->2->1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initia
2 min read
C Program For Finding The Middle Element Of A Given Linked List
Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list
4 min read
C Program For Deleting A Node In A Doubly Linked List
Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list
4 min read
C# Program For Deleting A Node In A Doubly Linked List
Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list
4 min read
C Program For Making Middle Node Head In A Linked List
Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5 Input: 1 2 3 4 5 6 Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves
3 min read
C Program For Deleting A Linked List Node At A Given Position
Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1
3 min read
C Program For Merge Sort For Doubly Linked List
Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l
3 min read
C Program For Inserting A Node In A Linked List
We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. C // A linked list node struct Node { int data; struct Node *next
7 min read
C Program For Deleting Last Occurrence Of An Item From Linked List
Using pointers, loop through the whole list and keep track of the node prior to the node containing the last occurrence key using a special pointer. After this just store the next of next of the special pointer, into to next of special pointer to remove the required node from the linked list. C #inc
4 min read