zoukankan      html  css  js  c++  java
  • LeetCode 138 Copy List with Random Pointer

    LeetCode 138. Copy List with Random Pointer

    又是copy 指针的题目。

    这个和上一道题目有个坑点,函数中的参数要加&地址符。

    class Solution {
    public:
        RandomListNode* ans;
        map<int,RandomListNode*> m;
        RandomListNode *copyRandomList(RandomListNode *head) {
            
           if(head==NULL)
               return ans;
           
            dfs(ans,head);
            return ans;
        }
        
        void dfs(RandomListNode* &ans,RandomListNode *head)
        {
            if(head==NULL) return;
            if(m[head->label]==NULL)
            {
                 m[head->label] = new RandomListNode(head->label);
            }
            ans = new RandomListNode(head->label);
            if(head->random==NULL)
                ans->random = NULL;
            else
            {
                if(m[head->random->label]==NULL)
                    m[head->random->label] = new RandomListNode(head->random->label);
                ans->random = m[head->random->label];
            }
    
            dfs(ans->next,head->next);
        }
    };
    
  • 相关阅读:
    表连接问题
    public interface Serializable?标记/标签接口
    4.21
    第十周周记
    测试
    第九周周记
    第七周周记
    fighting.
    fighting
    作业一
  • 原文地址:https://www.cnblogs.com/dacc123/p/9963356.html
Copyright © 2011-2022 走看看