2个有序
http://www.cnblogs.com/grandyang/p/4086297.html
k个有序
https://www.cnblogs.com/grandyang/p/4606710.html
题目:链表A和B都升序,合并为一个升序的链表。
思路:递归
#include <iostream>
using namespace std;
typedef struct Node
{
int val;
struct Node* next;
}node;
node* merge(node* a,node* b)
{
if(a==NULL)
return b;
if(b==NULL)
return a;
node* head=NULL;
if(a->val>b->val)
{
head=b;
head->next=merge(a,b->next);
}
else
{
head=a;
head->next=merge(a->next,b);
}
return head;
}
int main()
{
node* n1=new node;
node* n2=new node;
node* n3=new node;
n1->val=1;
n2->val=2;
n3->val=3;
n1->next=n2;
n2->next=NULL;
n3->next=NULL;
node* h=new node;
h=merge(n1,n3);
while(h!=NULL)
{
cout<<h->val<<" ";
h=h->next;
}
}