leetcode148 C++ 336ms 简化版的单链表排序


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int quick_sort(ListNode* begin, ListNode* end, int temp){
        if(begin == end){
            return 0;
        }
        int key = begin->val;
        ListNode* left = begin;
        ListNode* right = left->next;
        while(right !=end){
            if(right->val < key){
                left = left->next;
                temp = left->val;
                left->val = right->val;
                right->val = temp;
            }
            right = right->next;
        }
        temp = left->val;
        left->val = begin->val;
        begin->val = temp;
        quick_sort(begin, left, 0);
        quick_sort(left->next, end, 0);
        return 0;
        
    };

    ListNode* sortList(ListNode* head) {
        quick_sort(head, nullptr, 0);
        return head;
    }
};

posted @ 2018-07-28 22:18  一条图图犬  阅读(356)  评论(0编辑  收藏  举报