原题链接
算法
AC代码(c++)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==NULL&&l2==NULL)
return NULL;
ListNode* L=new ListNode(0);
ListNode* head=L;
while(l1!=NULL&&l2!=NULL)
{
if(l1->val<=l2->val)
{
L->next=l1;
L=L->next;
l1=l1->next;
}
else{
L->next=l2;
L=L->next;
l2=l2->next;
}
}
while(l1!=NULL)
{
L->next=l1;
L=L->next;
l1=l1->next;
}
while(l2!=NULL)
{
L->next=l2;
L=L->next;
l2=l2->next;
}
return head->next;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
int len = lists.size();
if(len==0)
return NULL;
int j=1;
while(j<len)
{
for(int i=0;i<len-j;i=i+j*2)
{
lists[i]=mergeTwoLists(lists[i],lists[i+j]);
cout<<i<<" ";
}
cout<<endl;
j=j*2;
}
return lists[0];
}
};