-
Notifications
You must be signed in to change notification settings - Fork 8
/
mergesort-linkedlist.cpp
79 lines (72 loc) · 1.36 KB
/
mergesort-linkedlist.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
// https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/1
#include <bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node *next;
};
Node *getNode(int data){
Node *temp = new Node();
temp->data = data;
temp->next = NULL;
return temp;
}
void split(Node *head, Node **front, Node **back){
if(!head || !head->next){
*front = head;
*back = NULL;
}else{
Node *slow = head;
Node *fast = head->next;
while(fast){
fast = fast->next;
if(fast){
slow = slow->next;
fast = fast->next;
}
}
*front = head;
*back = slow->next;
slow->next = NULL;
}
}
Node * sortedMerge(Node *a, Node *b){
if(!a)
return b;
else if(!b)
return a;
Node *res = NULL;
if(a->data <= b->data){
res = a;
res->next = sortedMerge(a->next, b);
}else{
res = b;
res->next = sortedMerge(a, b->next);
}
return res;
}
void mergeSort(Node **headRef){
Node *head = *headRef;
Node *a, *b;
if(!head || !head->next)
return;
split(head, &a, &b);
mergeSort(&a);
mergeSort(&b);
*headRef = sortedMerge(a,b);
}
int main(){
Node *head = getNode(5);
head->next = getNode(1);
head->next->next = getNode(7);
head->next->next->next = getNode(2);
head->next->next->next->next = getNode(9);
head->next->next->next->next->next = getNode(3);
mergeSort(&head);
Node *p = head;
while(p){
cout << p->data << " ";
p = p->next;
}
return 0;
}