leetcode——链表快排

来源:互联网 发布:电力工程计价软件 编辑:程序博客网 时间:2024/05/17 23:00
#include <iostream>using namespace std;  struct ListNode {      int val;      ListNode *next;      ListNode(int x) : val(x), next(NULL) {}  }; class Solution {public:        void swapp ( int & x,int &y){        int t = x;        x = y;        y = t;    }    void quickSort(ListNode * begin,ListNode * end){        //终止条件        if( begin == end || begin->next == end){            return;        }        //divide        int pivot = begin->val;        ListNode *p = begin;        ListNode *q = begin->next;        while ( q!= end){            if( q->val < pivot){                p = p->next;                swapp(p->val,q->val);            }        q = q->next;        }        swapp(begin->val,p->val);                //conquer        quickSort( begin,p);        quickSort(p->next,end);    }    ListNode *sortList(ListNode *head) {        quickSort(head,NULL);        return head;    }};int main(){}

0 0
原创粉丝点击