zoukankan      html  css  js  c++  java
  • 【LeetCode】138. 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.

    提示:

    此题有两种方法,一种是按照原链表next的顺序依次创建节点,并处理好新链表的next指针,同时把原节点与新节点的对应关系保存到一个hash_map中,然后第二次循环将random指针处理好。这种方法的时间复杂度是O(n),空间复杂度也是O(n)。

    第二种方法则是在原链表的每个节点之后插入一个新的节点,这样原节点与新节点的对应关系就已经明确了,因此不需要用hash_map保存,但是需要第三次循环将整个链表拆分成两个。这种方法的时间复杂度是O(n),空间复杂度是O(1)。

    但是利用hash_map的方法具有其他的优点,如果在循环中加入一个判断,就可以检测出链表中是否有循环;而第二种方法则不行,会陷入死循环。

    代码:

    使用hash_map的方法:

    /**
     * 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) {
            if (!head) return NULL;
            unordered_map<RandomListNode*, RandomListNode*> mp;
            // 创建一个新的链表头
            RandomListNode *new_head = new RandomListNode(head->label);
            // node1负责指向原链表,node2负责指向新链表
            RandomListNode *node1 = head, *node2 = new_head;
            /**
             * 按照原链表的结构不断创建新的节点,并维护好next指针,将node1与node2的对应关系保存到hash_map中,
             * 以备后面维护random指针的时候,可以通过node1找到对应的node2。
             */
            while (node1->next != NULL) {
                mp[node1] = node2;
                node1 = node1->next;
                node2->next = new RandomListNode(node1->label);
                node2 = node2->next;
            }
            // 将两个链表的尾巴的对应关系也保存好
            mp[node1] = node2;
            
            // 继续从头开始处理random指针
            node1 = head;
            node2 = new_head;
            while (node1->next != NULL) {
                node2->random = mp[node1->random];
                node1 = node1->next;
                node2 = node2->next;
            }
            // 把尾巴的random指针也处理好
            node2->random = mp[node1->random];
            return new_head;
        }
    };

    不使用hash_map的方法:

    /**
     * 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) {
            /**
             * 假设:l1代表原链表中的节点;l2代表新链表中的节点
             */
            RandomListNode *new_head, *l1, *l2;
            if (head == NULL) return NULL;
            
            /**
             * 第一步:在每一个l1后面创建一个l2,并让l1指向l2,l2指向下一个l1;
             */
            for (l1 = head; l1 != NULL; l1 = l1->next->next) {
                l2 = new RandomListNode(l1->label);
                l2->next = l1->next;
                l1->next = l2;
            }
            
            /**
             * 第二步:给l2的random赋值,l1的random的next指向的就是l2的random的目标;
             */
            new_head = head->next;
            for (l1 = head; l1 != NULL; l1 = l1->next->next) {
                if (l1->random != NULL) l1->next->random = l1->random->next;
            }
            
            /**
             * 第三步:需要将整个链表拆成两个链表,具体做法是让l1的next指向后面的后面;
             *         l2的next也指向后面的后面。
             */
            for (l1 = head; l1 != NULL; l1 = l1->next) {
                l2 = l1->next;
                l1->next = l2->next;
                if (l2->next != NULL) l2->next = l2->next->next;
            }
            return new_head;
        }
    };
  • 相关阅读:
    (转)创建Windows服务(Windows Services)N种方式总结
    无法加载协定为“ServiceReference1.xxxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分。
    《App架构实践指南》
    Awesome Projects (汇聚全球所有🐮项目,你值得拥有)
    【公告】个人站点及系列文章
    Android+TensorFlow+CNN+MNIST 手写数字识别实现
    TensorFlow基础
    UiAutomator2.0升级填坑记
    那些年,从博客到出书的博主
    Appuim源码剖析(Bootstrap)
  • 原文地址:https://www.cnblogs.com/jdneo/p/4748972.html
Copyright © 2011-2022 走看看