第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 ...
随机推荐
- (转)Python中的split()函数的用法
Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...
- php错误等级
E_ALL - 所有的错误和警告(不包括 E_STRICT) E_ERROR - 致命性的运行时错误 E_WARNING - 运行时警告(非致命性错误) E_PARSE - 编译时解析错误 ...
- 基础7 面向对象进阶与socket编程
1.静态方法(用得少)(解除某个函数跟类的关联,加了静态方法后,类便不能将类的参数传给静态方法函数了) class Dog(object): def __init__(self,name): @sta ...
- Visual Studio 要求导入 pfx 密钥以及导入后依然要求导入的解决办法
本文为个人博客备份文章,原文地址: http://validvoid.net/visual-studio-pfx-import/ 导入密钥 在使用 Visual Studio 生产项目时,使用 pfx ...
- NPOI 操作EXCEL 小计
由于需要做一个生成下载Excel的功能,查了一下 常用的操作有 NPOI Spire DOCX,于是便下载了NPOI试了一下,发现确实好用,但是还是有几个比较坑的地方 1.不能直接删除列 虽然提供了 ...
- .NET开发人员必知的八个网站
当前全球有数百万的开发人员在使用微软的.NET技术.如果你是其中之一,或者想要成为其中之一的话,我下面将要列出的每一个站点都应该是你的最爱,都应该收藏到书签中去.对于不熟悉.NET技术的朋友,需要说明 ...
- poj 2378 删点最大分支不超过一半
http://poj.org/problem?id=2378 这题和找重心基本一样,判断条件换一下就行 #include <iostream> #include <string> ...
- intellijidea课程 intellijidea神器使用技巧 3-3 postfix
Ctrl shift A ==> postfix completion 调出postfix 方法体中 ==> for 100.fori ==>enter for循环10 ...
- elasticsearch排序-----5
我们之前查询出的结果都会有一个_score分值表示列出结果与搜索结果的相关性,该值越高排序位置越靠前,es具体是如何计算该值的,我们认真来看看. 1.根据字段值排序 比如我们要查询/index5下su ...
- Unable to copy a file from obj\Debug to bin\Debug
1. Exit the VS2012, and then re-open the solution. 2. Clean the solution and build.