第35题:LeetCode138. Copy List with Random Pointer
题目
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。
要求返回这个链表的深度拷贝。
考点
思路
代码
/**
* 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的更多相关文章
- Leetcode138. Copy List with Random Pointer复制带随机指针的链表
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 方法一: class Solution { public: RandomLis ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 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 ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- [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 ...
- LeetCode138:Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- 第8章 IO类
8.1 IO类 iostream istream, wistream从流中读取数据 ostream, wostream iostream, wiostream读写 ...
- mysql 8 修改root 密码
主要参考:https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html 需要注意的是创建文件的时候需要保存为 utf-8 无 B ...
- linux_api之文件属性
本篇索引:1.引言2.文件类型3.获取文件属性的函数,stat.fstat.lstat4.超级用户(root用户)和普通用户5.进程与用户ID6.文件权限的检查7.新创建的的文件和目录的所有权8.ac ...
- Linux大文件快速处理小方法
背景 工作中使用MapReduce任务导出一批含有路径的文件,共计行数300W+,需要检测文件是否在对应的服务器中存在,而文件所在的服务器并非hadoop集群的服务器,因此打算采用bash脚本进行.具 ...
- 关于数学问题的urls
一个知乎账号, 分析了很多的数学问题: https://www.zhihu.com/people/matongxue/activities 关于三阶样条的解析: https://blog.csdn.n ...
- 部署Cube报错,用户登录失败;280000
在创建SSAS项目过程中,创建数据源.数据源视图.多维数据集.纬度等一切都没有问题.但是在“进程”这一步的时候,发现总是报错,提示如下. OLE DB 错误: OLE DB 或 ODBC 错误 : 用 ...
- Python函数(3)
一.装饰器 什么是装饰器,装饰器就是用于拓展原来函数功能的一种函数 装饰器就是用来为被装饰对象添加新功能的工具,装饰器本身可以是任意可调用对象,被装饰的对象也可以是任意可调用对象 装饰器遵循一个关键原 ...
- CSS Grid 布局学习笔记
CSS Grid 布局学习笔记 好久没有写博客了, MDN 上关于 Grid 布局的知识比较零散, 正好根据我这几个月的实践对 CSS Grid 布局做一个总结, 以备查阅. 1. 基础用法 Grid ...
- .net 使用Memcached
1.创建个人MemcachedHelper类 /// <summary> /// 页 面 名:缓存管理类<br/> /// 说 明:设置.获取.移除Cache<br/&g ...
- Asp.net防御XSS攻击组件库
一.AntiXss 翻看mvc4高级编程,偶看到作者强烈推荐使用AntiXss防御XSS攻击,收集资料看下. 目前类库已融入到.netframework中,类库主页不再更新. 使用方法:使用Nuget ...