输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
错误写法:if(head1 == NULL || head2 == NULL) return NULL;没有考虑有剩余数
这题用递归!!书写起来比较简洁
ListNode* Merge(ListNode* head1, ListNode* head2){
if(head1 == nullptr)return head2;
if(head2 == nullptr)return head1;
ListNode* result = nullptr;
if(head1->val > head2->val){
result = head2;
result->next = Merge(head1, head2->next);
}else{
result = head1;
result->next = Merge(head1->next, head2);
}
return result;
}