快排:
class Solution {public: ListNode* solve(ListNode* head, ListNode* pend){ if(head==NULL||head->next==pend) return head; int val = head->val; ListNode* p = head; ListNode* q = p->next; while(q!=pend){ if(q->val < val){ p = p->next; swap(p->val, q->val); } q = q->next; } swap(head->val, p->val); return p; } void quick_sort(ListNode* head, ListNode* pend){ if(head==pend) return; ListNode* p = solve(head, pend);//找出枢轴 quick_sort(head, p); quick_sort(p->next, pend); } ListNode* sortInList(ListNode* head) { // write code here quick_sort(head, NULL); return head; }};
利用vector和sort进行排序
class Solution {public: ListNode* sortInList(ListNode* head) { vector