leetcode——链表快排

#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()
{
}

你可能感兴趣的:(LeetCode,C++)