Flatten a multilevel linked list
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.
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 say that we need to flatten level by level. The idea of solution is, we start from first level, process all nodes one by one, if a node has a child, then we append the child at the end of list, otherwise we don’t do anything. After the first level is processed, all next level nodes will be appended after first level. Same process is followed for the appended nodes.
1) Take "cur" pointer, which will point to head of the fist 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 C implementation of the above algorithm.
// 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("\n");
}
// 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;
}
}
// A driver program to test above functions
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.
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Intelligent
Also we should make cur->child = NULL; before cur = cur->next; otherwise the main list is still pointing to their earlier child node.
Here is a code using queue in O(n) time complexity
cur = head
while(1)
if(cur==NULL)
break;
if cur has a child, enqueue(child).
if(cur->next == NULL)
cur->next = dequeue(q);
cur = cur->next;
@Hanish
I think you miss one condition of checking the empty queue,we will stop the execution when queue has no elements.
else your nice approach ..!
When queue becomes empty, dequeue(q) will return NULL;
=> current->next = NULL;
current = current->next (= NULL);
if(current == NULL)
break;
since initially queue is empty, we cant break if queue is NULL.
Complete working code using queues. Enjoy
// Program to flatten list with next and child pointers #include <stdio.h> #include <stdlib.h> #include<iostream> #include<queue> using namespace std; // 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; }; queue<struct node*>myqueue; // 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("\n"); } // 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=head; myqueue.push(head); while(!myqueue.empty()) { struct node *cur=myqueue.front(); myqueue.pop(); while(cur!=NULL) { cout<<cur->data<<" "; if(cur->child!=NULL) myqueue.push(cur->child); cur=cur->next; } } return; } // A driver program to test above functions int main(void) { struct node *head = NULL; head = createlist(); flattenList(head); cout<<endl; //printList(head); return 0; }In the given example what happens if I made 2 connections like
child of 12 is 13
AND
13 next is 17
In this case as per the given algorithm, some part of the list will be added more than once... what do you say..?
Please correct me if I am wrong..!
/* Paste your code here (You may delete these lines if not writing code) Node* flatten(Node* head){ if(!head) return head; Node* prev = NULL; Node* result = NULL; Queue* Q = createQueue(); Q->enqueue(head); while(Q->isEmpty == false) { Node* temp = Q->dequeue(); if(prev) prev->next = temp; if(!result) result = temp; while(temp->next) { if(temp->child) Q->enqueue(temp->child); temp = temp->next; } prev = temp; } return result; } */This is the simplest method (of using queues) rather than using the pointers.
This is a working code:
http://codepad.org/yd52skUK
I think it will not work if last node of a level has child
because
while(temp->next)
will be false if temp is last node of level,
and if it has child, then this child will not be pushed into queue.
Correct me, if I am wrong.
test case: 12
|
13
Only 2 nodes: 13 is child of 12
What if it forms a cycle?for instance 7 11 4 20 13 17 7
pls reply admin
i have doubt what if child pointer not point to head of next level??
how can u obtain head of next level in that case??
@admin plzz reply ASAP
@ aayushkumar........its simple..
suppose 10 points to 20 as its child node...
then we can specify it by writing...
head1->child=head2->next;
but this you r telling to create the list but how to flatten such a list ?
The function for flattening linked list using Queue is as follows:
private static NodeNovel flattenLinkedList(NodeNovel head){ LinkedList<NodeNovel> queue=new LinkedList<NodeNovel>(); queue.addLast(head); NodeNovel headAns=null, headAnsCur=null; NodeNovel curNode; headAns=new NodeNovel(head.val); headAnsCur=headAns; while(queue.size()>0){ curNode=queue.removeFirst(); while(curNode!=null){ if(curNode.val!=headAns.val){ headAnsCur.next=new NodeNovel(curNode.val); headAnsCur=headAnsCur.next; } queue.addLast(curNode.child); curNode=curNode.next; } } return headAns; } class NodeNovel{ int val; NodeNovel child, next; public NodeNovel(int val){ this.val=val; this.child=null; this.next=null; } }