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

    题解:

    Solution 1 ()

    class Solution {
    public:
        RandomListNode *copyRandomList(RandomListNode *head) {
            if (head == nullptr) return nullptr;
            RandomListNode* dummy = new RandomListNode(-1);
            RandomListNode* cur = head;
            RandomListNode* node = dummy;
            unordered_map<RandomListNode*, RandomListNode*> map;
            while (cur) {
                RandomListNode* tmp = new RandomListNode(cur->label);
                node->next = tmp;
                map[cur] = node->next;
                node = node->next;
                cur = cur->next;
            }
            node = dummy->next;
            cur = head;
            while (node) {
                node->random = map[cur->random];
                node = node->next;
                cur = cur->next;
            }
            
            return dummy->next;
        }
    };

    Solution 2 ()

    class Solution {
    public:
        RandomListNode *copyRandomList(RandomListNode *head) {
            if (!head) return head;
            RandomListNode* cur = head;
            while (cur) {
                RandomListNode* node = new RandomListNode(cur->label);
                node->next = cur->next;
                cur->next = node;
                cur = node->next;
            }
            cur = head;
            while (cur) {
                if (cur->random) {
                    cur->next->random = cur->random->next;
                }
                cur = cur->next->next;
            }
            cur = head;
            RandomListNode* first = cur->next;
            while (cur) {
                RandomListNode* tmp = cur->next;
                cur->next = tmp->next;
                if (tmp->next) {
                    tmp->next = tmp->next->next;
                }
                cur = cur->next;  
            }
            
            return first;
        }
    };

    Solution 3 ()

    class Solution {
    public:
        RandomListNode *copyRandomList(RandomListNode *head) {
            RandomListNode *newHead, *l1, *l2;
            if (head == NULL) return NULL;
    
            for (l1 = head; l1 != NULL; l1 = l1->next) {
                l2 = new RandomListNode(l1->label);
                l2->next = l1->random;
                l1->random = l2;
            }
            
            newHead = head->random;
            for (l1 = head; l1 != NULL; l1 = l1->next) {
                l2 = l1->random;
                l2->random = l2->next ? l2->next->random : NULL;
            }
            
            for (l1 = head; l1 != NULL; l1 = l1->next) {
                l2 = l1->random;
                l1->random = l2->next;
                l2->next = l1->next ? l1->next->random : NULL;
            }
    
            return newHead;
        }
    };
  • 相关阅读:
    golang学习之旅:官方文档汇总
    golang学习之旅:搭建go语言开发环境
    《编码》读书笔记:从无到有构建计算机系统
    在Eclipse中如何关联spring-framework的文档和源代码
    Java中Unicode的编码和实现
    个人常用工具整理
    battery-historian结果分析
    电量分析工具 Battery Historian 的配置及使用
    appium使用常见问题汇总--持续更新
    appium连接夜神模拟器方法总结
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6849032.html
Copyright © 2011-2022 走看看