题解 | #priority_queue(堆)单链表的排序#
单链表的排序
https://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 priority_queue<int, vector<int>, greater<int> > heap; ListNode *p = head; //把链表中的元素全部压入小根堆, while (p) { heap.push(p->val); p=p->next; } ListNode *ymm = new ListNode(-1); p = ymm; while (!heap.empty()) { ListNode *temp = new ListNode(heap.top()); heap.pop(); p->next = temp; p=temp; } return ymm->next; } };