合并两个排序的链表

合并两个排序的链表,第1张

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */

/**
 * 
 * @param pHead1 ListNode类 
 * @param pHead2 ListNode类 
 * @return ListNode类
 */
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
    // write code here
    if(pHead1==NULL)
        return pHead2;
    if (pHead2==NULL)
        return pHead1;
    struct ListNode *temp = malloc(sizeof(struct ListNode));
    temp->next=NULL;
    if(pHead1->val<=pHead2->val){
        temp=pHead1;
        temp->next=Merge(pHead1->next,pHead2);
    }
    else{
        temp=pHead2;
        temp->next=Merge(pHead1,pHead2->next);
    }
    return temp;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1498870.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-25
下一篇 2022-06-25

发表评论

登录后才能评论

评论列表(0条)

保存