Skip to content

Commit 90aca7d

Browse files
authored
Merge pull request div-bargali#615 from heri2468/patch-5
Flatten_List.cpp added
2 parents d22a6ea + 4e722f2 commit 90aca7d

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
Given a Linked List of size N, where every node represents a linked list and contains two pointers of its type:
3+
(i) a next pointer to the next node,
4+
(ii) a bottom pointer to a linked list where this node is head.
5+
6+
Note: The flattened list will be printed using the bottom pointer instead of next pointer.
7+
8+
9+
*/
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
struct Node{
14+
int data;
15+
struct Node * next;
16+
struct Node * bottom;
17+
18+
Node(int x){
19+
data = x;
20+
next = NULL;
21+
bottom = NULL;
22+
}
23+
24+
};
25+
26+
void printList(Node *Node)
27+
{
28+
while (Node != NULL)
29+
{
30+
printf("%d ", Node->data);
31+
Node = Node->bottom;
32+
}
33+
}
34+
35+
Node* flatten (Node* root); //function for flattening a multilevel linked list
36+
37+
int main(void) {
38+
39+
int t;
40+
cin>>t; // number of test cases
41+
while(t--){
42+
int n,m,flag=1,flag1=1;
43+
struct Node * temp=NULL;
44+
struct Node * head=NULL;
45+
struct Node * pre=NULL;
46+
struct Node * tempB=NULL;
47+
struct Node * preB=NULL;
48+
cin>>n;
49+
int work[n];
50+
for(int i=0;i<n;i++)
51+
cin>>work[i];
52+
for(int i=0;i<n;i++){
53+
m=work[i];
54+
--m;
55+
int data;
56+
scanf("%d",&data);
57+
temp = new Node(data);
58+
temp->next = NULL;
59+
temp->bottom = NULL;
60+
61+
if(flag){
62+
head = temp;
63+
pre = temp;
64+
flag = 0;
65+
flag1 = 1;
66+
}
67+
else{
68+
pre->next = temp;
69+
pre = temp;
70+
flag1 = 1;
71+
}
72+
for(int j=0;j<m;j++){
73+
74+
int temp_data;
75+
scanf("%d",&temp_data);
76+
tempB = new Node(temp_data);
77+
78+
if(flag1){
79+
temp->bottom=tempB;
80+
preB=tempB;
81+
flag1=0;
82+
}
83+
else{
84+
preB->bottom=tempB;
85+
preB=tempB;
86+
}
87+
}
88+
}
89+
Node *fun = head;
90+
Node *fun2=head;
91+
92+
Node* root = flatten(head);
93+
printList(root);
94+
cout<<endl;
95+
96+
}
97+
return 0;
98+
}
99+
// } Driver Code Ends
100+
101+
102+
/* Node structure used in the program
103+
104+
struct Node{
105+
int data;
106+
struct Node * next;
107+
struct Node * bottom;
108+
109+
Node(int x){
110+
data = x;
111+
next = NULL;
112+
bottom = NULL;
113+
}
114+
115+
};
116+
*/
117+
118+
Node *merge(Node *a,Node *b)
119+
{
120+
if(a==NULL)
121+
return b;
122+
if(b==NULL)
123+
return a;
124+
Node *result;
125+
if(a->data<b->data)
126+
{
127+
result =a;
128+
result->bottom = merge(a->bottom,b);
129+
}
130+
else
131+
{
132+
result=b;
133+
result->bottom = merge(a,b->bottom);
134+
}
135+
return result;
136+
}
137+
/* Function which returns the root of
138+
the flattened linked list. */
139+
Node *flatten(Node *root)
140+
{
141+
// Your code here
142+
if(root==NULL||root->next==NULL) return root;
143+
144+
return merge(root,flatten(root->next));
145+
}
146+
147+
/*
148+
Approach used is to merge the bottom node with the current node.
149+
*/

0 commit comments

Comments
 (0)