题目

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。

要求返回这个链表的深度拷贝。


考点


思路


代码

/**
* 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) {
RandomListNode* ptr=head;
RandomListNode* new_head=NULL;
std::vector<RandomListNode*> node_vec;
std::map<RandomListNode*,int>node_map;
int i=0; while(ptr){ node_map[ptr]=i;
node_vec.push_back(new RandomListNode(ptr->label)); i++;
ptr=ptr->next;
} ptr=head;
node_vec.push_back(0);
i=0;
while(ptr){
node_vec[i]->next=node_vec[i+1];
if(ptr->random){
int id=node_map[ptr->random];
node_vec[i]->random=node_vec[id];
}
i++;
ptr=ptr->next;
}
return node_vec[0];
}
};

问题

第35题:LeetCode138. Copy List with Random Pointer的更多相关文章

  1. Leetcode138. Copy List with Random Pointer复制带随机指针的链表

    给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 方法一: class Solution { public: RandomLis ...

  2. 16. Copy List with Random Pointer

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

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

  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. 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. [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. Sphinx Building Docs in horizon

    Building Contributor Documentation This documentation is written by contributors, for contributors. ...

  2. 通过rsync+inotify实现数据的实时备份(多台备份机)

    在前面的博文中,我讲到过利用rsync实现数据的镜像和备份,但是要实现数据的实时备份,单独靠rsync还不能实现,本文就讲述下如何实现数据的实时备份. 一.rsync的优点与不足  与传统的cp.ta ...

  3. PHP算法——生成唯一字符串

    经常遇到忘记密码的情况,点击一下忘记密码,然后收到更改密码的链接,连接中往往都会有一段很长而且很乱的字符串.试想一下,如果出现了重复的字符串,那岂不是把别人的密码给重置了? 所以产生唯一数对于网站的安 ...

  4. Flyweight_pattern--reference

    http://en.wikipedia.org/wiki/Flyweight_pattern In computer programming, flyweight is a software desi ...

  5. Tips In C

    C语言中的使用操作 宏定义时使用do while防止语句的分离, 但是不使用与需要有返回值的语句, 这个时候可以参考第二条 宏定义时使用({}), ()加上{}的方式, 在代码中填写逻辑算法, 最后的 ...

  6. net 总数据中取随机几条数据

    List<string> lstSample = new List<string>(); Random rand = new Random(); List<int> ...

  7. npm 包下载很慢的解决办法

    原因: 国内访问外网都很慢,甚至不能访问!安装Node时自带的npm地址默认是:http://registry.npmjs.org  三种方法: 1.通过config命令 npm config set ...

  8. Entity Framework中IQueryable, IEnumerable, IList的区别[转]

    使用工具追踪EF生成的SQL 使用Entity Framework等ORM框架的时候,SQL对于使用者来说是透明的,往往很多人也不关心ORM所生成的SQL,然而系统出现性能问题的时候就必须关注生成的S ...

  9. 服务器返回的14种常见HTTP状态码

    当我们从客户端向服务器发送请求时 服务器向我们返回状态码 状态码就是告诉我们服务器响应的状态 通过它,我们就可以知道当前请求是成功了还是出现了什么问题 状态码是由3位数字和原因短语组成的(比如最常见的 ...

  10. android api 之Scroller

    Scroller是封装了滚动,实现View和ViewGroup的背景画布的滚动. 它有两个构造方法: public Scroller (Context context) 传递一个上下文. public ...