• 【LeetCode】138. Copy List with Random Pointer


    Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

    Return a deep copy of the list.

    BFS遍历链表,类似树的层次遍历,进行深度复制。

    /**
     * Definition for singly-linked list with a random pointer.
     * struct RandomListNode {
     *     int label;
     *     RandomListNode *next, *random;
     *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
     * };
     */
    class Solution {
    public:
        RandomListNode *copyRandomList(RandomListNode *head) {
            //BFS
            if(head == NULL)
                return NULL;
            unordered_map<RandomListNode*, RandomListNode*> m;
            unordered_map<RandomListNode*, bool> visited;
            queue<RandomListNode*> q;
            q.push(head);
            visited[head] = true;
            while(!q.empty())
            {
                RandomListNode* front = q.front();
                q.pop();
                RandomListNode* copyfront;
                if(m.find(front) == m.end())
                {
                    copyfront = new RandomListNode(front->label);
                    m[front] = copyfront;
                }
                else
                    copyfront = m[front];
                if(front->random)
                {
                    RandomListNode* random = front->random;
                    if(visited[random] == false)
                    {
                        q.push(random);
                        visited[random] = true;
                    }
                    RandomListNode* copyrandom;
                    if(m.find(random) == m.end())
                    {
                        copyrandom = new RandomListNode(random->label);
                        m[random] = copyrandom;
                    }
                    else
                        copyrandom = m[random];
                    copyfront->random = copyrandom;
                }
                if(front->next)
                {
                    RandomListNode* next = front->next;
                    if(visited[next] == false)
                    {
                        q.push(next);
                        visited[next] = true;
                    }
                    RandomListNode* copynext;
                    if(m.find(next) == m.end())
                    {
                        copynext = new RandomListNode(next->label);
                        m[next] = copynext;
                    }
                    else
                        copynext = m[next];
                    copyfront->next = copynext;
                }
            }
            return m[head];
        }
    };

  • 相关阅读:
    关于使用JavaMail注册激活邮箱的注意点
    Maven Web报错:org.apache.jasper.JasperException: Unable to compile class for JSP
    IDEA的中文乱码问题
    深入了解Java动态代理与反射机制
    String、StringBuffer和StringBuilder的区别
    Java中HashCode()和equals()的关系
    Java中向下转型的意义
    局部内部类访问局部变量的问题
    Java内部类的应用场景
    Python os.rmdir() 方法
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4105734.html
Copyright © 2020-2023  润新知