输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
ListNode* head=new ListNode(-1);
ListNode* dummy=head;
for(;pHead1!=NULL||pHead2!=NULL;){
int value1=pHead1==NULL?INT_MAX:pHead1->val;
int value2=pHead2==NULL?INT_MAX:pHead2->val;
if(value1<value2){
head->next=pHead1;
pHead1=pHead1==NULL?NULL:pHead1->next;
}
else{
head->next=pHead2;
pHead2=pHead2==NULL?NULL:pHead2->next;
}
head=head->next;
}
return dummy->next;
}
};