两个有序链表的合并
class Solution{
public:
Node * myMerge(Node * list1, Node * list2){
if(list1 == null) return list2;
if(list2 == null) return list1;
Node * head = new Node();
Node * p = list1, * q = list2, * m = head;
while(p != null && q != null){
if(p->data < q->data){
m->next = p;
p = p->next;
}
else{
m->next = q;
q = q->next;
}
m = m->next;
m->next = null;
}
while(p != null)
m->next = p;
while(q != null)
m->next = q;
return head;
}
Node * recursiveMerge(Node * list1, Node * list2){
Node * temp = null;
if(!list1) return list2;
if(!list2) return list1;
if(list1->data < list2->data){
temp = list1;
temp->next = recursiveMerge(list1->next,list2);
}
else{
temp = list2;
temp->next = recursiveMerge(list1,list2->next);
}
return temp;
}
};