题目:

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(-);
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;
}
};

【Lintcode】105.Copy List with Random Pointer的更多相关文章

  1. 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告

    题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...

  2. 【LeetCode】138. Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  3. 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...

  4. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  5. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  6. 【概率论】3-1:随机变量和分布(Random Variables and Discrete Distributions)

    title: [概率论]3-1:随机变量和分布(Random Variables and Discrete Distributions) categories: Mathematic Probabil ...

  7. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  8. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  9. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

随机推荐

  1. python3 configparser对配置文件读写

    import configparser #read data from conf filecf=configparser.ConfigParser()cf.read("biosver.cfg ...

  2. SpringBoot启动流程分析(四):IoC容器的初始化过程

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  3. gulp入门-压缩js/css文件(windows)

    类似于grunt,都是基于Node.js的前端构建工具.不过gulp压缩效率更高. 工具/原料 nodejs/npm 方法/步骤 首先要确保pc上装有node,然后在global环境和项目文件中都in ...

  4. hashCode与equals的作用与区别及应当注意的细节

    最近去面试了几家公司,被问到hashCode的作用,虽然回答出来了,但是自己还是对hashCode和equals的作用一知半解的,所以决定把它们研究一下. 以前写程序一直没有注意hashCode的作用 ...

  5. python升级安装后的yum的修复

    升级python版本号后,执行yum # yum -y install openssl 提演示样例如以下: There was a problem importing one of the Pytho ...

  6. 【设计模式】C++单例模式的几种写法——Java自动加载内部类对象,C++怎么破?

    单例模式是最简单的设计模式,就让我像玩简单的游戏一样写下去吧. v1: 简单模式 和这个版本有过一面之缘,但不敢苟同. class Singleton { private: Singleton() { ...

  7. selenium3 踩坑--move_to_element()报错

    问题:selenium3 使用move_to_element()报错,报错信息如下图所示: 网上没有找到合适的解决办法,回退到稳定的selenium2可以解决. pip install seleniu ...

  8. 【转】windows下 ADT NDK开发环境配置

    前提: 下载好Ecplise ADT并配置好开发环境,不会配置环境可以参考这里: http://blog.csdn.net/danfengw/article/details/47111107 步骤: ...

  9. Android UI开发第四十三篇——使用Property Animation实现墨迹天气3.0引导界面及动画实现

    前面写过<墨迹天气3.0引导界面及动画实现>,里面完美实现了动画效果,那一篇文章使用的View Animation,这一篇文章使用的Property Animation实现.Propert ...

  10. 【BZOJ1499】[NOI2005]瑰丽华尔兹 单调队列+DP

    [BZOJ1499][NOI2005]瑰丽华尔兹 Description 你跳过华尔兹吗?当音乐响起,当你随着旋律滑动舞步,是不是有一种漫步仙境的惬意?众所周知,跳华尔兹时,最重要的是有好的音乐.但是 ...