zoukankan      html  css  js  c++  java
  • 复杂链表的复制

    原文地址:https://www.jianshu.com/p/a0c983c292b1

    时间限制:1秒 空间限制:32768K

    题目描述

    输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

    我的代码

    /*
    struct RandomListNode {
        int label;
        struct RandomListNode *next, *random;
        RandomListNode(int x) :
                label(x), next(NULL), random(NULL) {
        }
    };
    */
    class Solution {
    public:
        RandomListNode* Clone(RandomListNode* pHead)
        {
            if(pHead==nullptr)
                return nullptr;
            RandomListNode* pHead1=pHead;
            RandomListNode* pHead2=new RandomListNode(pHead->label);
            RandomListNode* cloneHead=pHead2;
            map<RandomListNode*,RandomListNode*> m;
            m[pHead1]=pHead2;
            while(pHead1){
                if(pHead1->next)
                    pHead2->next=new RandomListNode(pHead1->next->label);
                else
                    pHead2->next=nullptr;
                pHead1=pHead1->next;
                pHead2=pHead2->next;
                m[pHead1]=pHead2;
            }
            pHead1=pHead;pHead2=cloneHead;
            while(pHead1){
                pHead2->random=m[pHead1->random];
                pHead1=pHead1->next;
                pHead2=pHead2->next;
            }
            return cloneHead;
        }
    };
    

    运行时间:3ms
    占用内存:484k

    /*
    struct RandomListNode {
        int label;
        struct RandomListNode *next, *random;
        RandomListNode(int x) :
                label(x), next(NULL), random(NULL) {
        }
    };
    */
    class Solution {
    public:
        RandomListNode* Clone(RandomListNode* pHead)
        {
            if(pHead==nullptr)
                return nullptr;
            RandomListNode* cur=pHead;
            //复制
            while(cur){
                RandomListNode* node=new RandomListNode(cur->label);
                node->next=cur->next;
                cur->next=node;
                cur=node->next;
            }
            cur=pHead;
            while(cur){
                RandomListNode* node=cur->next;
                if(cur->random)
                    node->random=cur->random->next;
                cur=node->next;
            }
            //拆分
            RandomListNode* cloneHead=pHead->next;
            cur=pHead;
            while(cur->next){
                RandomListNode* node=cur->next;
                cur->next=cur->next->next;
                cur=node;
            }
            return cloneHead;
        }
    };
    

    运行时间:3ms
    占用内存:484k

  • 相关阅读:
    php网站请求返回结果被加入恶意代码片段
    php tp5 的index.php 被改成首页静态页面内容
    gitee https 方式每次都要输入密码的解决
    linux 下 php执行 exec 无反应 返回值为空
    layerconfirm 右上角关闭事件等于按钮2
    微信小程序公共方法创建与调用
    php curl 转为 x-www-form-urlencoded 方式
    php 加载 zip 文件
    小程序语音播报
    mysql命令行操作-将查询结果导出到文件
  • 原文地址:https://www.cnblogs.com/cherrychenlee/p/10796704.html
Copyright © 2011-2022 走看看