/**
* 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
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326250566&siteId=291194637
Recommended
Ranking