将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:输入:1->2->4,1->3->4
输出:1->1->2->3->4->4
typedef struct ListNode Node;
struct ListNode* mergeTwoLists(struct ListNode* L1, struct ListNode* L2)
{
if(NULL == L1)
return L2;
if(NULL == L2)
return L1;
Node head;
Node* pTail = &head;//head是结构体类型的变量
Node* PL1 = L1;
Node* PL2 = L2;
while(PL1 && PL2)
{
if(PL1->val <= PL2->val) //val值域
{
pTail->next = PL1;
PL1 = PL1->next;
}
else
{
pTail->next = PL2;
PL2 = PL2->next;
}
pTail = pTail->next;
}
if(NULL == PL1)
pTail->next = PL2;
else
pTail->next = PL1;
return head.next;
}
~bye~