输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
来源:力扣(LeetCode)
完全一致
21. 合并两个有序链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
if (l1 == NULL)
{
return l2;
}
else if (l2 == NULL)
{
return l1;
}
struct ListNode *mergeListHead = NULL;
if (l1 -> val < l2 -> val)
{
mergeListHead = l1;
mergeListHead -> next = mergeTwoLists(l1 -> next, l2);
}
else
{
mergeListHead = l2;
mergeListHead -> next = mergeTwoLists(l1 , l2 -> next);
}
return mergeListHead;
}