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.

这题用map做其实比较简单,但是一开始没想明白越想越乱,最后看了下别人的实现,思路还是很清晰的,代码如下所示:

 /**
* 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) {
unordered_map<RandomListNode *, RandomListNode *> m;
RandomListNode helper1(), * p1 = &helper1, helper2(), *p2 = &helper2;
p1->next = head;
while(p1->next){
p1 = p1->next;
RandomListNode * tmpNode = new RandomListNode(p1->label);
m[p1] = tmpNode;
p2 = p2->next = tmpNode;
}
p1 = &helper1;
while(p1->next){
p1 = p1->next;
if(p1->random){
m[p1]->random = m[p1->random];
}
}
return helper2.next;
}
};

LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)的更多相关文章

  1. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

  2. [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表

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

  3. [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表

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

  4. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

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

  5. 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现

    题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...

  6. [LeetCode OJ] Copy List with Random Pointer 扩大

    职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意:对一个有回路的链表的深复制 解题:这道题我AC了之后才发 ...

  7. [Leetcode Week17]Copy List with Random Pointer

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

  8. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

  9. Java for LeetCode 138 Copy List with Random Pointer

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

随机推荐

  1. Bag类的接口的实现与测试(课上测试补做)

    Bag类的接口的实现与测试(课上测试补做) 截图 由于截图有一定的的限制就没有吧所有的代码截进去,后面有代码. 代码 package ClassTest; import java.util.Objec ...

  2. word里怎么删除某一列

    光标定位在第二列第一个字的前面,然后按住Alt键,拖动鼠标,选中第二列字,松开Alt键,点击Delete键即可

  3. C++ 读取文件所有内容的方法

    方法一 #include <fstream> #include <string> #include <iostream> using namespace std; ...

  4. UVa 11149 矩阵的幂(矩阵倍增法模板题)

    https://vjudge.net/problem/UVA-11149 题意: 输入一个n×n矩阵A,计算A+A^2+A^3+...A^k的值. 思路: 矩阵倍增法. 处理方法如下,一直化简下去直到 ...

  5. Asp.net WebApi 配置 Swagger UI

    首先安装Swashbuckle.Core 然后添加swagger配置文件. [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), &q ...

  6. [C# 开发技巧系列]如何动态设置屏幕分辨率

    首先,大家应该明确,现在没有可用的API来给我们动态地设置屏幕分辨率,我们要实现这个需求,我们只能在C#程序中调用Win32 API 函数来解决这个问题的,这里用C#代码调用Win32 API 就涉及 ...

  7. HttpServlet实现serializable

    Java Servlet Technology Overview Servlets are the Java platform technology of choice for extending a ...

  8. ADO.net笔记

    1.DbConnectionConnection对象也称为数据库连接对象,Connection对象的功能是负责对数据源的连接.所有Connection对象的基类都是DbConnection类.Conn ...

  9. byte[]与各种数据类型互相转换示例

    public class TestCase { /** * short到字节数组的转换. */ public static byte[] shortToByte(short number) { int ...

  10. css实现标题文字过长截取...

    css实现网页中文字过长截取... title class应该这样写: .title{ width:300px; white-space:nowrap; overflow:hidden; text-o ...