题解 | #合并k个已排序的链表#
合并k个已排序的链表
http://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6
列表 vector 链表之间的转换
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
int k = lists.size();
vector<int>nodes;
for (int i = 0; i < k; i++) {
ListNode *cur = lists[i];
while (cur) {
nodes.push_back(cur->val);
cur = cur->next;
}
}
sort(nodes.begin(), nodes.end());
ListNode *cur = new ListNode(0);
ListNode *res = cur;
for(int i=0;i<nodes.size();i++){
cur->next = new ListNode(nodes[i]);
cur = cur->next;
}
return res->next;
}
};