forked from itsjohnty/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort_linkedlist.cpp
More file actions
78 lines (69 loc) · 1.86 KB
/
Sort_linkedlist.cpp
File metadata and controls
78 lines (69 loc) · 1.86 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
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
private:
ListNode* mergeList(ListNode* node1, ListNode* node2){
ListNode* fh = NULL, *ft = NULL;
while(node1 and node2){
if(!fh and !ft){
if(node1 -> val > node2 -> val){
fh = node2;
ft = node2;
node2 = node2 -> next;
}
else{
fh = node1;
ft = node1;
node1 = node1 -> next;
}
}
if(node1 and node2){
if(node1 -> val < node2 -> val){
ft -> next = node1;
ft = ft -> next;
node1 = node1 -> next;
}
else{
ft -> next = node2;
ft = ft -> next;
node2 = node2 -> next;
}
}
}
if(node1){
ft -> next = node1;
}
if(node2){
ft -> next = node2;
}
return fh;
}
public:
ListNode* sortList(ListNode* head) {
if(!head || !head -> next) return head;
ListNode* slow = head;
ListNode* fast = head;
ListNode* temp = new ListNode();
while(fast && fast -> next){
temp = slow;
slow = slow -> next;
fast = fast -> next -> next;
}
temp -> next = nullptr;
ListNode* l1 = sortList(head);
ListNode* l2 = sortList(slow);
return mergeList(l1, l2);
}
};
int main()
{
cout<<"Code in the backend of compiler\n";
return 0;
}