forked from philona/cppcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergerSortLinkedList.cpp
More file actions
114 lines (107 loc) · 2.47 KB
/
MergerSortLinkedList.cpp
File metadata and controls
114 lines (107 loc) · 2.47 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
/*Merge Sort for Linked List
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
*/
#include<iostream>
using namespace std;
struct node{
int data;
node* next;
};
void push(node** head, int l_data){
node* temp = new node();
temp->data = l_data;
temp->next=NULL;
if(*head == NULL){
*head = temp;
return;
}
node*n = *head;
while(n->next!=NULL)
n = n->next;
n->next=temp;
}
void findMiddleElement(node* curr, node**first, node**second){
node* slow= curr;
node* fast = curr->next;
while(fast!=NULL){
fast=fast->next;
while(fast!=NULL){
slow=slow->next;
fast=fast->next;
}
}
*first = curr;
*second = slow->next;
slow->next=NULL;
}
node* mergeBoth(node* first, node* second){
node* answer = NULL;
if(!first)
return second;
if(!second)
return first;
if(first->data <= second->data){
answer = first;
answer->next = mergeBoth(first->next, second);
}else{
answer = second;
answer->next = mergeBoth(first, second->next);
}
return answer;
}
void MergeSorting(node**head){
node* curr= *head;
node* first;
node* second;
if(!curr || !curr->next) return;
findMiddleElement(curr, &first, &second);
MergeSorting(&first);
MergeSorting(&second);
*head = mergeBoth(first, second);
}
node* mergeSort(node* head){
MergeSorting(&head);
return head;
}
void print(node* n){
while(n!=NULL){
cout<<n->data<<" ";
n = n->next;
}
cout<<"\n";
}
int main(){
node* head = NULL;
int n, ele;
cout<<"\n Enter the size of the llist : ";
cin>>n;
cout<<"\n Enter elements to be entered in linked list: ";
for(int i=0; i<n; i++){
cin>>ele;
push(&head, ele);
}
cout<<"\n The llist 1 is: ";
print(head);
head = mergeSort(head);
cout<<"\n merge sort of ll: ";
print(head);
}