zoukankan      html  css  js  c++  java
  • leetcode138

    /**
     * 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;
        }
    };
  • 相关阅读:
    Activity的singleTask启动模式
    蓝图Tips
    UE4 C++ Tips
    UE4的抛物线
    透明材质
    在UE4 C++构造函数里的常规操作
    UE4的反射关键词
    UE4字符串的各种转换
    UE4Launch遇到的问题
    在UE4C++中的宏
  • 原文地址:https://www.cnblogs.com/asenyang/p/9826727.html
Copyright © 2011-2022 走看看