题目

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) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if ( !head ) return head;
std::map<RandomListNode *, RandomListNode *> oriNext;
RandomListNode *pOri = head;
RandomListNode dummy(-);
RandomListNode *pCopy = &dummy;
// first round visit
while (pOri)
{
// store the original Node's next pointer
oriNext[pOri] = pOri->next;
// create copy Node
pOri->next = new RandomListNode(pOri->label);
pCopy->next = pOri->next;
pCopy->next->random = pOri;
pCopy = pCopy->next;
// move to the next Node
pOri = oriNext[pOri];
}
// second round visit
pCopy = dummy.next;
while (pCopy)
{
pCopy->random = pCopy->random->random ? pCopy->random->random->next : NULL;
pCopy = pCopy->next;
}
// third round recover original list
for (std::map<RandomListNode *, RandomListNode *>::iterator i = oriNext.begin(); i != oriNext.end(); ++i)
{
i->first->next = i->second;
}
return dummy.next;
}
};

Tips

典型的链表深拷贝,之前用python做的时候用了O(1)空间技巧(http://www.cnblogs.com/xbf9xbf/p/4216655.html)。

这次尝试了用hashmap的O(n)空间的技巧,原因是感觉hashmap可能更通用一些。

思路主要参考了下面这个日志:http://www.geeksforgeeks.org/a-linked-list-with-next-and-arbit-pointer/

需要注意的是:如果原来的链表节点random为空,就不要再往下寻找next;需要加一个保护判断

===============================================================

第二次过这道题,hashmap的大体思路画画图还记得,代码调试了几次才AC。

(1)不要忘记恢复原来的链表

(2)在拷贝random的时候,不要忘记copy = copy->next这条语句

/**
* 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;
map<RandomListNode*, RandomListNode*> ori_next;
RandomListNode* p = head;
RandomListNode dummpy(-);
RandomListNode* copy = &dummpy;
while (p)
{
copy->next = new RandomListNode(p->label);
copy->next->random = p;
ori_next[p] = p->next;
p = p->next;
copy->next->random->next = copy->next;
copy = copy->next;
}
copy = dummpy.next;
while (copy)
{
copy->random = copy->random->random ? copy->random->random->next : NULL;
copy = copy->next;
}
for ( map<RandomListNode*, RandomListNode*>::iterator i=ori_next.begin(); i!=ori_next.end(); ++i )
{
i->first->next = ori_next[i->first];
}
return dummpy.next;
}
};

【Copy List with Random Pointer】cpp的更多相关文章

  1. leetcode 【 Copy List with Random Pointer 】 python 实现

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

  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 Week17]Copy List with Random Pointer

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

  8. 【leetcode】Copy List with Random Pointer (hard)

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

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

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

随机推荐

  1. 使用startForeground让android服务前台运行

    最近在使用android 4.1系统的时候,发现在手机休眠一段时间后(1-2小时),后台运行的服务被强行kill掉,有可能是系统回收内存的一种机制,要想避免这种情况可以通过startForegroun ...

  2. Check Point R80 Security Management

    平台: CentOS 类型: 虚拟机镜像 软件包: Security Management basic software security 服务优惠价: 按服务商许可协议 云服务器费用:查看费用 立即 ...

  3. python3基础08(exec、bytearray使用等)

    #!/usr/bin/env python# -*- coding:utf-8 -*- str="test"print(ascii(str))a=bytearray("a ...

  4. UOJ#126【NOI2013】快餐店

    [NOI2013]快餐店 链接:http://uoj.ac/problem/126 YY了一个线段树+类旋转卡壳的算法.骗了55分.还比不上$O(n^2)$暴力T^T 题目实际上是要找一条链的两个端点 ...

  5. linux 命令——11 nl (转)

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...

  6. codeforce Gym 100425E The Street Escalator(期望,线性递推)

    算数学期望,每个人都可以分开来考虑.Xi表示第i个人跑到另外一边的次数. Xi服从二项分布.概率的和是个二项式,(p+1-p)^T,把二项式展开,p的偶次项是留在原来那一边的概率. 可以用((a+b) ...

  7. Android(java)学习笔记74:ListViewProject案例(ListView + ArrayAdapter)

    1. 首先是MainActivity.java文件,如下: package com.himi.lv1; import java.util.ArrayList; import java.util.Lis ...

  8. python_54_函数调用函数

    logger函数的定义要放在函数调用之前,在test1(1,2)前边,一下两种都可以 def test1(x,y): print(x,y) logger('Test1') def logger(sou ...

  9. Xcode Warning: “no rule to process file

    警告⚠️: warning: no rule to process file '/Users/Kingdev/Desktop/Git/finance_iOS/finance/Library/MBpro ...

  10. SummerVocation_Learning--java的线程死锁

    public class Test_DeadLock implements Runnable { ; static Object o1 = new Object(),o2 = new Object() ...