Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路: 这道题目的思路比较好想,和K=2时的场景差不多。利用priority_queue这个STL类,很好的解决了选取k个节点最小值的问题。采用二级指针构造排序链表 代码: struct cmp { bool operator()(ListNode *x, ListNode *y) { return x->val > y->val; } }; class Solution { public: ListNode *mergeKLists(vector<ListNode *> &lists) { ListNode *ret = NULL; ListNode **pcur = &ret; ListNode *tmp = NULL; if (lists.size() == 0) { return ret; } priority_queue<ListNode *, vector<ListNode *>, cmp> que; for (int i = 0; i < lists.size(); i++) { if (lists[i] != NULL) { que.push(lists[i]); } } while (!que.empty()) { tmp = que.top(); que.pop(); *pcur = tmp; pcur = &((*pcur)->next); if (tmp->next != NULL) { que.push(tmp->next); } } return ret; } }; 注: Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition. This context is similar to a heap where only the max heap element can be retrieved (the one at the top in the priority queue) and elements can be inserted indefinitelly.