题解 | #单链表的排序#
单链表的排序
http://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 the head node * @return ListNode类 */ ListNode* sortInList(ListNode* head) { // write code here vector<int> s; while(head) { s.push_back(head->val); head = head->next; } sort(s.begin(), s.end(), greater<int>()); auto p = new ListNode(-1); for(int i = 0; i < s.size(); i++) { auto q = new ListNode(s[i]); q->next = p->next; p->next = q; } return p->next; } };