zoukankan      html  css  js  c++  java
  • 【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];
        }
    };

  • 相关阅读:
    Django之url路由
    Django之setting文件
    Diango之通过form表单向服务端发送数据
    Django之win7下安装与命令行工具
    Linux学习之查看系统资源命令总结(二十二)
    实现简单的web框架
    Linux下发送邮件
    Linux学习之日志管理(二十一)
    Shell学习之结合正则表达式与通配符的使用(五)
    Linux学习之后台任务与定时任务(二十)
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4105734.html
Copyright © 2011-2022 走看看