https://leetcode.cn/problems/merge-two-sorted-lists/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution
{
public:
ListNode* mergeTwoLists(ListNode *head1, ListNode *head2)
{
ListNode *head3 = new ListNode(-1);
ListNode *p = head3;
while(head1 != nullptr && head2 != nullptr)
{
if(head1->val < head2->val)
{
p->next = head1;
head1 = head1->next;
}
else
{
p->next = head2;
head2 = head2->next;
}
p = p->next;
}
if(head1 != nullptr)
{
p->next = head1;
}
else
{
p->next = head2;
}
return head3->next;
}
};