/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Acception of Mine
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0) return NULL;
ListNode* p = lists[0];
for(int i = 1; i<lists.size(); i++)
{
ListNode* q = lists[i];
p = helper(p,q);
}
return p;
}
private:
ListNode* helper(ListNode* p ,ListNode* q)
{
ListNode* head = new ListNode(0);
ListNode* ret = head;
while(p != NULL && q != NULL)
{
if(p->val <=q->val)
{
head->next = p;
head = p;
p = p->next;
}
else
{
head->next = q;
head = q;
q= q->next;
}
}
if(NULL == p) head->next = q;
if(NULL == q) head->next = p;
ListNode* pTemp = ret;
ret = ret->next;
delete(pTemp);
pTemp = NULL;
return ret;
}
};
23. Merge k Sorted Lists
猜你喜欢
转载自blog.csdn.net/bjzhaoxiao/article/details/80177422
今日推荐
周排行