【leetcode】合并两个有序链表

 

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){  
    struct ListNode* head = (struct ListNode*)calloc(sizeof(struct ListNode),1);
    struct ListNode* p = head;
    while(l1 && l2)
    {
        if(l1->val <= l2->val)
        {
            p->next = l1;
            l1 = l1->next;
        }
        else
        {
            p->next = l2;
            l2 = l2->next;         
        }
        p = p->next;
    }
    p->next = (l1)? l1: l2;
    return head->next;
}

 

posted @ 2020-09-19 00:35  温暖了寂寞  阅读(119)  评论(0编辑  收藏  举报