struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
if (l1 == NULL && l2 == NULL)
return NULL;
else if(l1 == NULL)
return l2;
else if(l2 == NULL)
return l1;
struct ListNode* head;
struct ListNode* tail;
if (l1->val <= l2->val) // 找出两个链表第一个值最小值选择做头部开始
{
head = l1;
tail = l2;
}
else
{
head = l2;
tail = l1;
}
struct ListNode* ret = head; // 用来返回的指针
struct ListNode* p; // 循环里用来转换的
while(head->next!=NULL && tail!=NULL)
{
if (tail->val <= head->next->val)
{
p = tail->next;
tail->next = head->next;
head->next = tail;
tail = p;
head = head->next;
}
else
head = head->next;
}
if (head->next == NULL)
{
head->next = tail;
}
return ret;
}