题目地址:

https://oj.leetcode.com/problems/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.

/**
* 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) {}
* };

*/

方法:

难点在于random指针需要指向一个复制过程中还不存在的结点。解决办法就简单了,先复制完单链表,再处理每个结点的random指针,这样复制random指针时其指向的结点就已经存在了。

为了高效完成这个工作,我们需要建立一个映射,以旧地址为键,新地址为值,方便复制random指针。

全部代码:

class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head)
return NULL;
unordered_map<int,int> dict;
RandomListNode *tmpHead = head;
RandomListNode *resHead = NULL;
RandomListNode *now = NULL;
RandomListNode *nxt = NULL;
resHead = (RandomListNode *)malloc(sizeof(RandomListNode));
dict[(int)head] = (int)resHead;
resHead->label = tmpHead->label;
resHead->next = NULL;
resHead->random = NULL;
now = resHead;
tmpHead = tmpHead->next;
while (tmpHead)
{
nxt = (RandomListNode *)malloc(sizeof(RandomListNode));
nxt->label = tmpHead->label;
nxt->next = NULL;
nxt->random = NULL;
dict[(int)tmpHead] = (int)nxt;
now->next = nxt;
now = nxt;
tmpHead = tmpHead->next;
}
tmpHead = head;
while (tmpHead)
{
now = (RandomListNode *)dict[(int)tmpHead];
if (tmpHead->random != NULL)
now->random = (RandomListNode *)dict[(int)(tmpHead->random)]; tmpHead = tmpHead->next;
}
return resHead;
}
};

【原创】leetCodeOj --- Copy List with Random Pointer 解题报告的更多相关文章

  1. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

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

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

  3. 16. Copy List with Random Pointer

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

  4. 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 ...

  5. Copy List with Random Pointer leetcode java

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

  6. LintCode - Copy List with Random Pointer

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

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

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

  8. [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. LeetCode138:Copy List with Random Pointer

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

随机推荐

  1. Cocos2d-x 3.0 创建一个场景,并设置现场的时候,项目开始执行上主动

    头 #ifndef __TEST_H__ #define __TEST_H__ #include "cocos2d.h" USING_NS_CC; class Test : pub ...

  2. ThinkPHP分页使用例子(二十一)

    原文:ThinkPHP分页使用例子(二十一) ThinkPHP分页使用 PHP代码: public function fenye(){ $User = M('Leyangjun'); // 实例化Us ...

  3. 那些年我们装过的数据库---盘点sqlserver2008安装时遇到的各种的问题(持续更新中)

    给自己安过sqlServer2008,也给好多同学安过sqlServer2008,期间遇到了好多不同的另人心烦的问题,在这里整理一下,(涉及到的部分方法是在网上找的,有些也没试过,仅仅是在这里整理一下 ...

  4. Android ARM汇编语言

    简介 ARM是Advanced RISC Machine的首字母缩写,它可以称之为一家嵌入式处理器的提供商,也可以理解为一种处理器的架构,还可以将它作为一套完整的处理器指令集. 原生程序与ARM汇编语 ...

  5. Android开发5:布局管理器2(表格布局TableLayout)

    版本:Android4.3 API18  学习整理:liuxinming 概念      TableLayout继承了LinearLayout,因此它的本质依然是线性布局管理器.      表格布局采 ...

  6. HDU 4611 Balls Rearrangement (数学-思维逻辑题)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...

  7. hdoj 1258 SUM IT UP

    程序的思想是:输入数据是,先使用快排对其从大到小进行排序,然后记录相同数据的个数,比如4 3 3 2 2 1 1,最后的数据变成4 3 2 1 ,并且同时数据的个数f[]变成1 2 2 2 然后就是遍 ...

  8. &lt;xliff:g&gt;标签

    摘要: 这是Android4.3Mms源代码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sm ...

  9. MFC模态对话框的消息循环

    MFC模态对话框的消息循环 单线程程序, 当主窗口响应函数中弹出模态对话框时,为什么主窗口响应函数可能照常工作? 当弹出模态对话框时,线程的消息循环无法返回,父窗口的事件本应没人处理,应该处于卡死状态 ...

  10. Mac下配置Cocos2d-x3.1环境

    一.前期准备 1.ADT:百度下就OK 2.NDK:百度下就OK 3.ANT: http://124.254.47.39/download/55152992/78533365/4/zip/57/132 ...