-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlattening a Linked List.cpp
More file actions
136 lines (114 loc) · 3.3 KB
/
Flattening a Linked List.cpp
File metadata and controls
136 lines (114 loc) · 3.3 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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *child;
// Constructors to initialize the
// data, next, and child pointers
Node() : data(0), next(nullptr), child(nullptr) {};
Node(int x) : data(x), next(nullptr), child(nullptr) {}
Node(int x, Node *nextNode, Node *childNode) : data(x), next(nextNode), child(childNode) {}
};
// Merges two linked lists in a particular
// order based on the data value
Node* merge(Node* list1, Node* list2){
// Create a dummy node as a
// placeholder for the result
Node* dummyNode = new Node(-1);
Node* res = dummyNode;
// Merge the lists based on data values
while(list1 != NULL && list2 != NULL){
if(list1->data < list2->data){
res->child = list1;
res = list1;
list1 = list1->child;
}
else{
res->child = list2;
res = list2;
list2 = list2->child;
}
res->next = NULL;
}
// Connect the remaining
// elements if any
if(list1){
res->child = list1;
} else {
res->child = list2;
}
// Break the last node's
// link to prevent cycles
if(dummyNode->child){
dummyNode->child->next = NULL;
}
return dummyNode->child;
}
// Flattens a linked list with child pointers
Node* flattenLinkedList(Node* head){
// If head is null or there
// is no next node, return head
if(head == NULL || head->next == NULL){
return head;
}
// Recursively flatten the
// rest of the linked list
Node* mergedHead = flattenLinkedList(head->next);
head = merge(head, mergedHead);
return head;
}
// Print the linked list by
// traversing through child pointers
void printLinkedList(Node* head) {
while (head != nullptr) {
cout << head->data << " ";
head = head->child;
}
cout << endl;
}
// Print the linked list
// in a grid-like structure
void printOriginalLinkedList(Node* head, int depth) {
while (head != nullptr) {
cout << head->data;
// If child exists, recursively
// print it with indentation
if (head->child) {
cout << " -> ";
printOriginalLinkedList(head->child, depth + 1);
}
// Add vertical bars
// for each level in the grid
if (head->next) {
cout << endl;
for (int i = 0; i < depth; ++i) {
cout << "| ";
}
}
head = head->next;
}
}
int main() {
// Create a linked list with child pointers
Node* head = new Node(5);
head->child = new Node(14);
head->next = new Node(10);
head->next->child = new Node(4);
head->next->next = new Node(12);
head->next->next->child = new Node(20);
head->next->next->child->child = new Node(13);
head->next->next->next = new Node(7);
head->next->next->next->child = new Node(17);
// Print the original
// linked list structure
cout << "Original linked list:" << endl;
printOriginalLinkedList(head, 0);
// Flatten the linked list
// and print the flattened list
Node* flattened = flattenLinkedList(head);
cout << "\nFlattened linked list: ";
printLinkedList(flattened);
return 0;
}