题目:

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. 上门洗车APP --- Androidclient开发 之 网络框架封装介绍(二)

    上门洗车APP --- Androidclient开发 之 网络框架封装介绍(二) 前几篇博文中给大家介绍了一下APP中的基本业务及开发本项目使用的网络架构: 上门洗车APP --- Androidc ...

  2. Android应用开发:网络工具——Volley(一)

    引言 网络一直是我个人的盲点,前一阵子抽空学习了一下Volley网络工具的用法,也透过源代码进行了进一步的学习,有一些心得想分享出来.在Android开发中,成熟的网络工具不少,Android自带了H ...

  3. matlab2016b-linux版本在ubutu16.04x64上面不能打开摄像头的处理方法

    this  can  not  work.    need  find  other way.   ================================================== ...

  4. nyist oj 37 回文字符串 (动态规划经典)

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 所谓回文字符串,就是一个字符串.从左到右读和从右到左读是全然一样的.比方"aba".当 ...

  5. c# .net Global.asax文件的作用

    1 Global.asax文件的作用 先看看MSDN的解释,Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选的文件,该文件包含响应 ASP.NET 或HTTP模块所引发的 ...

  6. SecureCRT的上传和下载

    securtCRT对于后台开发者并不陌生,在windows下是得力的助手.而文件从服务器上上传和下载是很基本.很日常的操作.下面就谈谈关于它的命令及操作: 借助securtCRT,使用linux命令s ...

  7. 常用yum命令小结

    基于rpm的软件包管理器 yum,是Yellow dog Updater, Modified的简称,是一种软件包管理器.它能够从指定的服务器自动下载RPM包并安装,可以自动处理依赖性关系,并且一次安装 ...

  8. UVALive 6530 Football (水

    题目链接:点击打开链 #include <cstdio> #include <vector> #include <algorithm> using namespac ...

  9. phpmyadmin内存溢出

    phpmyadmin Fatal error: Allowed memory size of 134217728 bytes 解决方法: 在报错的页面里,加上这句: ini_set('memory_l ...

  10. 箭头函数 this arrow function 无this

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions import w ...