• (剑指Offer)面试题17:合并两个排序的链表


    题目:

    输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然时按照递增排序的。

    链表结点定义如下:

    struct ListNode{
        int val;
        ListNode* next;
    };
    

    思路:

    合并两个递增排序的链表,思想类似于归并排序的merge过程。

    1、当两个链表均不为空,

    如果链表1头结点的值小于链表2头结点的值,那么链表1的头结点作为新链表的头结点,否则链表2的头结点作为新链表的头结点,链表指针往前走一步;

    对两个链表中剩余结点的操作同步骤1一样;(这是一个递归的过程)

    2、当两个链表至少有一个为空,

    新链表指针指向非空的那一个;

    代码:

    struct ListNode{
        int val;
        ListNode* next;
    };
    
    // recursive method
    ListNode* Merge_1(ListNode* pHead1, ListNode* pHead2){
        if(pHead1==NULL)
            return pHead2;
        if(pHead2==NULL)
            return pHead1;
        if(pHead1->val<pHead2->val){
            pHead1->next=Merge_1(pHead1->next,pHead2);
            return pHead1;
        }
        else{
            pHead2->next=Merge_1(pHead1,pHead2->next);
            return pHead2;
        }
    }
    
    // non-recursive method
    ListNode* Merge_2(ListNode* pHead1, ListNode* pHead2){
        ListNode *p=new ListNode();
        ListNode *MergeHead=p;
        while(pHead1!=NULL && pHead2!=NULL){
            if(pHead1->val<pHead2->val){
                MergeHead->next=pHead1;
                pHead1=pHead1->next;
            }
            else{
                MergeHead->next=pHead2;
                pHead2=pHead2->next;
            }
            MergeHead=MergeHead->next;
        }
    
        if(pHead1==NULL)
            MergeHead->next=pHead2;
        if(pHead2==NULL)
            MergeHead->next=pHead1;
    
        return p->next;
    }

    在线测试OJ:

    http://www.nowcoder.com/books/coding-interviews/d8b6b4358f774294a89de2a6ac4d9337?rp=1

    AC代码:

    class Solution {
    public:
        ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
        {
            if(pHead1==NULL)
                return pHead2;
            if(pHead2==NULL)
                return pHead1;
            if(pHead1->val<pHead2->val){
                pHead1->next=Merge(pHead1->next,pHead2);
                return pHead1;
            }
            else{
                pHead2->next=Merge(pHead1,pHead2->next);
                return pHead2;
            }
        }
    };
    
    class Solution {
    public:
        ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
        {
            ListNode *p=new ListNode(0);
            ListNode *MergeHead=p;
            while(pHead1!=NULL && pHead2!=NULL){
                if(pHead1->val<pHead2->val){
                    MergeHead->next=pHead1;
                    pHead1=pHead1->next;
                }
                else{
                    MergeHead->next=pHead2;
                    pHead2=pHead2->next;
                }
                MergeHead=MergeHead->next;
            }
    
            if(pHead1==NULL)
                MergeHead->next=pHead2;
            if(pHead2==NULL)
                MergeHead->next=pHead1;
    
            return p->next;
        }
    };
  • 相关阅读:
    BZOJ 1452 Count(二维树状数组)
    BZOJ 1407 Savage(拓展欧几里得)
    BZOJ 1415 聪聪和可可(期望DP)
    BZOJ 1406 密码箱(数论)
    最大流小结
    UVA6531Go up the ultras
    二分图小结
    Codeforces Round #243 (Div. 1)
    图论模板集合
    zoj3416 Balanced Number
  • 原文地址:https://www.cnblogs.com/AndyJee/p/4648404.html
Copyright © 2020-2023  润新知