File tree 1 file changed +44
-1
lines changed
1 file changed +44
-1
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ struct node {
4
+ int data;
5
+ node* next;
6
+ };
7
+ void insert (int data,node** head){
8
+ if (*head==NULL ){
9
+ node* n=new node;
10
+ n->data =data;
11
+ n->next =NULL ;
12
+ *head=n;
13
+ return ;
14
+ }
15
+ node* current=*head;
16
+ while (current->next !=NULL ){
17
+ current=current->next ;
18
+ }
19
+ node* n=new node;
20
+ n->data =data;
21
+ n->next =NULL ;
22
+ current->next =n;
23
+ }
24
+ void print (node* head){
25
+ node* current=head;
26
+ while (current!=NULL ){
27
+ cout<<current->data <<" " ;
28
+ current=current->next ;
29
+ }
30
+ cout<<endl;
31
+ }
1
32
node* split (node* head){
2
33
node* hare=head;
3
34
node* tortoise=head;
@@ -36,5 +67,17 @@ node* mergeSort(node* head){
36
67
if (!head || head->next ==NULL ){
37
68
return head;
38
69
}
39
- return converge (mergeSort (split (head)),mergeSort (head));
70
+ node* node1=head;
71
+ node* node2=split (head);
72
+ return converge (mergeSort (node1),mergeSort (split (head)));
73
+ }
74
+ int main (){
75
+ node* head=NULL ;
76
+ insert (2 ,&head);
77
+ insert (4 ,&head);
78
+ insert (3 ,&head);
79
+ insert (1 ,&head);
80
+ print (head);
81
+ head=mergeSort (head);
82
+ print (head);
40
83
}
You can’t perform that action at this time.
0 commit comments