C++ Program For Flattening A Multilevel Linked List
Last Updated :
22 Dec, 2021
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 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 a way that all nodes at the first level should come first, then nodes of the second level, and so on.
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 the "cur" pointer, which will point to the head
of the first level of the list
2) Take the "tail" pointer, which will point to the end of the
first level of the list
3) Repeat the below procedure while "curr" is not NULL.
I) If the 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 the new child list and update
the "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++
// C++ Program to flatten list with
// next and child pointers
#include <bits/stdc++.h>
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
class Node
{
public:
int data;
Node *next;
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
Node *createList(int *arr, int n)
{
Node *head = NULL;
Node *p;
int i;
for (i = 0; i < n; ++i)
{
if (head == NULL)
head = p = new Node();
else
{
p->next = new Node();
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(Node *head)
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout<<endl;
}
// This function creates the input
// list. The created list is same
// as shown in the above figure
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
Node *head1 = createList(arr1,
SIZE(arr1));
Node *head2 = createList(arr2,
SIZE(arr2));
Node *head3 = createList(arr3,
SIZE(arr3));
Node *head4 = createList(arr4,
SIZE(arr4));
Node *head5 = createList(arr5,
SIZE(arr5));
Node *head6 = createList(arr6,
SIZE(arr6));
Node *head7 = createList(arr7,
SIZE(arr7));
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(Node *head)
{
// Base case
if (head == NULL)
return;
Node *tmp;
/* Find tail node of first level
linked list */
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
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)
{
Node *head = NULL;
head = createList();
flattenList(head);
printList(head);
return 0;
}
// This code is contributed by rathbhupendra
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.
Please refer complete article on
Flatten a multilevel linked list for more details!
Similar Reads
Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the
8 min read
Linked List of Linked List Linked list of linkeÂd list, also known as nested linkeÂd lists, is a type of Linked List where each main node stores another full linked list. This structure beats old, plain linked lists. It gives way more flexibility to store and manage compleÂx data. In this article we will learn about the bas
9 min read
Iterative approach for removing middle points in a linked list of line segments This post explains the iterative approach of this problem. We maintain two pointers, prev and temp. If these two have either x or y same, we move forward till the equality holds and keep deleting the nodes in between. The node from which the equality started, we adjust the next pointer of that node.
9 min read
Rearrange a Linked List in Zig-Zag fashion | Set-2 Given a linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c.. are consecutive data node of linked list. Note that it is not allowed to swap data. Examples: Input: 1->2->3->4 Output: 1->3->2->4 Input: 11->
13 min read
Linked List in C++ In C++, a linked list is a linear data structure that allows the users to store data in non-contiguous memory locations. A linked list is defined as a collection of nodes where each node consists of two members which represents its value and a next/previous pointer which stores the address for the n
6 min read
Convert an Array to a Circular Doubly Linked List Prerequisite: Doubly Linked list, Circular Linked List, Circular Doubly Linked ListGiven an array of N elements. The task is to write a program to convert the array into a circular doubly linked list. The idea is to start traversing the array and for every array element create a new list node and as
8 min read