链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/
//1.哨兵位+双指针法(归并法)
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
if(list1==NULL)
{
return list2;
}
else if(list2==NULL)
{
return list1;
}
struct ListNode* head=(struct ListNode*)malloc(sizeof(struct ListNode));
if(head!=NULL)
head->next=NULL;
struct ListNode* tail=head;
struct ListNode* start1=list1;
struct ListNode* start2=list2;
while(start1&&start2)
{
if((start1->val)<(start2->val))
{
tail->next=start1;
tail=start1;
start1=start1->next;
}
else
{
tail->next=start2;
tail=start2;
start2=start2->next;
}
}
if(start1==NULL)
{
tail->next=start2;
}
else if(start2==NULL)
{
tail->next=start1;
}
struct ListNode* newhead=head->next;
free(head);
return newhead;
}