-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorttheLL.cpp
197 lines (140 loc) · 3.58 KB
/
SorttheLL.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//Sorting the LL
//Brute force approach
// #include<bits/stdc++.h>
// using namespace std;
// class Node {
// public :
// int data;
// Node* next;
// Node(int data1 , Node* next1){
// data = data1;
// next = next1;
// }
// Node(int data1){
// data = data1;
// next = nullptr;
// }
// };
// Node* sortLL(Node* head){
// //creating a array
// vector<int> arr;
// Node* temp = head;
// while(temp != nullptr){
// arr.push_back(temp->data);
// temp = temp->next;
// }
// // sort the array
// sort(arr.begin() , arr.end());
// temp = head;
// for(int i=0; i<arr.size(); i++){
// temp->data = arr[i];
// temp = temp->next;
// }
// return head;
// }
// //printing the linked list
// void printLL(Node* head){
// Node* temp = head;
// while(temp != nullptr){
// cout << temp->data << " ";
// temp = temp->next;
// }
// cout << endl;
// }
// int main() {
// Node* head = new Node(3);
// head->next = new Node(2);
// head->next->next = new Node(4);
// head->next->next->next = new Node(1);
// head->next->next->next->next = new Node(8);
// cout << "Original linked list: ";
// printLL(head);
// head = sortLL(head);
// cout << "sorted linked list: ";
// printLL(head);
// return 0;
// }
//time complexity = O(n + nlogn + n) , space complexity = O(n)
//Optimal solution using merge sort , fast/slow pointer.
#include<bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node(int data1 , Node* next1){
data = data1;
next = next1;
}
Node(int data1){
data = data1;
next = nullptr;
}
};
Node* merge2SortedLL(Node* list1 , Node* list2){
Node* dummyNode = new Node(-1);
Node* temp = dummyNode;
while(list1 != nullptr && list2 != nullptr) {
if(list1->data <= list2->data){
temp->next = list1;
list1 = list1->next;
} else {
temp->next = list2;
list2 = list2->next;
}
temp = temp->next;
}
if(list1 != nullptr){
temp->next = list1;
} else {
temp->next = list2;
}
return dummyNode->next;
}
//function to find middle of the linked list
Node* findMiddle(Node* head){
if(head == nullptr || head->next == nullptr) {
return head;
}
Node* slow = head;
Node* fast = head->next;
while(fast != nullptr && fast->next != nullptr){
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
Node* sortLL(Node* head){
if(head == nullptr || head->next == nullptr) {
return head;
}
Node* middle = findMiddle(head);
Node* right = middle->next;
middle->next = nullptr;
Node* left = head;
left = sortLL(left);
right = sortLL(right);
return merge2SortedLL(left , right);
}
//function to print the linked list
void printLL(Node* head){
Node* temp = head;
while(temp != nullptr){
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main(){
Node* head = new Node(2);
head->next = new Node(1);
head->next->next = new Node(6);
head->next->next->next = new Node(9);
head->next->next->next->next = new Node(0);
cout << "Original LL: ";
printLL(head);
head = sortLL(head);
cout << "sorted LL: ";
printLL(head);
return 0;
}