/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *}; */class solution {Public:int temp=0; listnode* getpartition (listnode* pbegin, listnode* pEnd) {int key = pbegin->val; listnode* left = Pbegin; listnode* right = pbegin->next; while (right! = pEnd) {if (Right->val < key) {left = left->next; This->temp = right->val; Right->val = left->val; Left->val = this->temp; } right = right->next; } this->temp = left->val; Left->val = pbegin->val; Pbegin->val = this->temp; return left; } void Quick_sort (listnode* pbegin, listnode*pend) {if (Pbegin! = pEnd) {listnode* partition = Get Partition (Pbegin, pEnd); Quick_sort (Pbegin, partition); quick_sORT (Partition->next, pEnd); }} listnode* Sortlist (listnode* head) {quick_sort (head, nullptr); return head; }};
leetcode148 C + + 340ms single-linked list quick sort two pointers are left->right mobile