排序链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head) {
//对链表进行快速排序算法的详解
//使用快速排序的算法进行详细的改进,使用归并排序的算法
Qsort(head,NULL);
return head;
}
//合并两个子链表的算法
void Qsort(ListNode* head, ListNode* end)
{
if (head != end)
{
ListNode* partion_1 = partion(head, end);
Qsort(head, partion_1);
Qsort(partion_1->next, end);
}
}
ListNode* partion(ListNode* begin,ListNode* end)
{
ListNode* p=begin;
ListNode* cur=begin->next;
int key=begin->val;
while(cur!=end)
{
if(cur->val<key)
{
p=p->next;
swap(p->val,cur->val);
}
cur=cur->next;
}
swap(p->val,begin->val);
return p;
}
};