-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlist-flatten.cpp
143 lines (125 loc) · 3.22 KB
/
list-flatten.cpp
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
// http://www.geeksforgeeks.org/flattening-a-linked-list/
// iterative method
#include <iostream>
using namespace std;
class node {
public:
int data;
node *next, *down;
node(int data) {
this->data = data;
this->next = NULL;
this->down = NULL;
}
};
node* flattenList(node *head);
node* sortedMerge(node *a, node *b);
int countList(node *head);
node* addnodeDown(node *head, int data);
node* addnodeAcross(node *head, int data);
void printListAcross(node *head);
void printListDown(node *head);
node* flattenList(node *head) {
if (head == NULL)
return head;
node *down, *across = head;
int n = countList(head);
for (int i = 0; i < n; i++) {
down = across->down;
head = sortedMerge(head, down);
across = across->next;
}
return head;
}
node* sortedMerge(node *a, node *b) {
if (a == NULL) return b;
else if (b == NULL) return a;
node *head = NULL;
while (a != NULL && b != NULL) {
if (a->data < b->data) {
head = addnodeAcross(head, a->data);
a = a->next;
}
else {
head = addnodeAcross(head, b->data);
b = b->down;
}
}
if (a == NULL)
while (b != NULL) {
head = addnodeAcross(head, b->data);
b = b->down;
}
if (b == NULL)
while (a != NULL) {
head = addnodeAcross(head, a->data);
a = a->next;
}
return head;
}
int countList(node *head) {
if (head == NULL)
return 0;
int count = 0;
node *ptr = head;
while (ptr != NULL) {
count++;
ptr = ptr->next;
}
return count;
}
node* addnodeDown(node *head, int data) {
if (head == NULL)
return new node(data);
node *ptr = head;
while (ptr->down != NULL)
ptr = ptr->down;
ptr->down = new node(data);
return head;
}
node* addnodeAcross(node *head, int data) {
if (head == NULL)
return new node(data);
node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new node(data);
return head;
}
void printListAcross(node *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
void printListDown(node *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->down;
}
}
int main() {
node *head = NULL;
head = addnodeDown(head, 5);
head = addnodeDown(head, 7);
head = addnodeDown(head, 8);
head = addnodeDown(head, 30);
head->next = addnodeDown(head->next, 10);
head->next = addnodeDown(head->next, 20);
head->next->next = addnodeDown(head->next->next, 19);
head->next->next = addnodeDown(head->next->next, 22);
head->next->next = addnodeDown(head->next->next, 50);
head->next->next->next = addnodeDown(head->next->next->next, 28);
head->next->next->next = addnodeDown(head->next->next->next, 35);
head->next->next->next = addnodeDown(head->next->next->next, 40);
head->next->next->next = addnodeDown(head->next->next->next, 45);
cout<<"flattened: ";
head = flattenList(head);
printListAcross(head);
cout<<endl<<endl;
return 0;
}