题解 | #链表排序#用一个数组收集再排序即可
链表排序
https://www.nowcoder.com/practice/d75c232a0405427098a8d1627930bea6
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ vector<int> getarray(ListNode* head){ if(head==nullptr) return vector<int>{}; vector<int> arr; while(head){ arr.push_back(head->val); head=head->next; } return arr; } ListNode* sortList(ListNode* head) { ListNode* p=head; vector<int> arr=getarray(head); sort(arr.begin(),arr.end()); int i=0; while(p){ p->val=arr[i++]; p=p->next; } return head; } };