-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathFlatten-Linked-List.cpp
More file actions
144 lines (126 loc) · 3.24 KB
/
Flatten-Linked-List.cpp
File metadata and controls
144 lines (126 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Flattening a Linked List
Medium Accuracy: 33.91% Submissions: 67762 Points: 4
Given a Linked List of size N, where every node represents a sub-linked-list and contains two pointers:
(i) a next pointer to the next node,
(ii) a bottom pointer to a linked list where this node is head.
Each of the sub-linked-list is in sorted order.
Flatten the Link List such that all the nodes appear in a single level while maintaining the sorted order.
Note: The flattened list will be printed using the bottom pointer instead of next pointer.
Example 1:
Input:
5 -> 10 -> 19 -> 28
| | | |
7 20 22 35
| | |
8 50 40
| |
30 45
Output: 5-> 7-> 8- > 10 -> 19-> 20->
22-> 28-> 30-> 35-> 40-> 45-> 50.
Explanation:
The resultant linked lists has every
node in a single level.
(Note: | represents the bottom pointer.)
Example 2:
Input:
5 -> 10 -> 19 -> 28
| |
7 22
| |
8 50
|
30
Output: 5->7->8->10->19->20->22->30->50
Explanation:
The resultant linked lists has every
node in a single level.
(Note: | represents the bottom pointer.)
*/
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *right, *down;
};
Node* head ;
Node* merge(Node* a, Node* b)
{
if (a == NULL)
return b;
if (b == NULL)
return a;
Node* result;
if (a->data < b->data)
{
result = a;
result->down = merge(a->down, b);
}
else
{
result = b;
result->down = merge(a, b->down);
}
result->right = NULL;
return result;
}
Node* flatten(Node* root)
{
if (root == NULL || root->right == NULL)
return root;
return merge(root, flatten(root->right));
}
Node* push(Node* head_ref, int data)
{
Node* new_node = new Node();
new_node->data = data;
new_node->right = NULL;
new_node->down = head_ref;
head_ref = new_node;
return head_ref;
}
void printList()
{
Node* temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->down;
}
cout << endl;
}
// Driver code
int main()
{
/* Let us create the following linked list
5 -> 10 -> 19 -> 28
| | | |
V V V V
7 20 22 35
| | |
V V V
8 50 40
| |
V V
30 45
*/
head = push(head, 30);
head = push(head, 8);
head = push(head, 7);
head = push(head, 5);
head->right = push(head->right, 20);
head->right = push(head->right, 10);
head->right->right = push(head->right->right, 50);
head->right->right = push(head->right->right, 22);
head->right->right = push(head->right->right, 19);
head->right->right->right = push(head->right->right->right, 45);
head->right->right->right = push(head->right->right->right, 40);
head->right->right->right = push(head->right->right->right, 35);
head->right->right->right = push(head->right->right->right, 20);
// Flatten the list
head = flatten(head);
printList();
return 0;
}