/**
* Definition for singly-linked list.
* 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 {
public:
struct compare {
bool operator()(ListNode* a, ListNode* b) {
return a->val < b->val;
}
};
ListNode* sortList(ListNode* head) {
vector<ListNode*> vec;
while (head) {
vec.push_back(head);
head = head->next;
}
sort(vec.begin(), vec.end(), compare());
ListNode* res = new ListNode();
ListNode* temp = res;
for (int i = 0; i < vec.size(); i++) {
res->next = vec[i];
res = res->next;
}
return temp->next;
}
};