《剑指Offer》之“合并两个排序的链表”

本文介绍了一种将两个已排序的链表合并成一个有序链表的算法,提供了递归及两种非递归实现方法,并详细解释了每种方法的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述


输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

代码实现


//递归
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        ListNode* pMerge = NULL;
        if (pHead1==NULL)
            return pHead2;
        if (pHead2==NULL)
            return pHead1;
        if(pHead1->val<=pHead2->val)
        {
            pMerge = pHead1;
            pMerge->next = Merge(pHead1->next,pHead2);
        }
        else
        {
            pMerge = pHead2;
            pMerge->next = Merge(pHead1,pHead2->next);
        }
        return pMerge;
    }
};
//非递归(错解)
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        ListNode* all =nullptr;
        ListNode* p = nullptr;
        ListNode* q = nullptr;

        if(pHead1 == nullptr || pHead2 == nullptr ){
           if(pHead1 == nullptr && pHead2 == nullptr ){
               return nullptr;
           }
            if(pHead1 == nullptr){
            return nullptr;    
            }
            if(pHead2 == nullptr){
            return nullptr;    
            }

        }

        if(pHead1 -> val > pHead2 -> val){
                all = pHead2;
            }

        while(pHead1->next != nullptr && pHead2->next != nullptr ){
            if(pHead1->val >= pHead2->next-> val){
                q = pHead2;
               pHead2 = pHead2->next;
            }
            else{
                p = pHead1;
                pHead1 = pHead1->next;

                p->next = pHead2->next;
                pHead1->next = p;


                pHead1 = pHead1->next;               
            }

        }
        while(pHead1 != nullptr){
            if(pHead1->val <= pHead2->val){
                p = pHead1;
                pHead1 = pHead1->next;

                p->next = q->next;
                q->next = p;

                q = q->next;


            }
            else{
                pHead2->next = p;
                break;
            }
        }
        return all;
    }
};
//非递归正解
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if (pHead1 == NULL) return pHead2;
        if (pHead2 == NULL) return pHead1;
        ListNode* head = NULL;
        if (pHead1->val < pHead2->val) {
            head = pHead1;
            pHead1 = pHead1->next;
        }
        else {
            head = pHead2;
            pHead2 = pHead2->next;
        }
        ListNode* temp = head;
        while (pHead1 && pHead2) {
            if (pHead1->val < pHead2->val) {
                temp->next = pHead1;
                temp = temp->next;
                pHead1 = pHead1->next;
            }
            else {
                temp->next = pHead2;
                temp = temp->next;
                pHead2 = pHead2->next;
            }
        }
        if (pHead1)
            temp->next = pHead1;
        if (pHead2)
            temp->next = pHead2;
        return head;
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值