第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 ...
随机推荐
- webview的进度条的加载,webview的使用以及handle的理解与使用
Webview的几个关键方法要介绍一些: 谷歌官方文档是这么说的; A WebView has several customization points where you can add your ...
- (转) awk学习
awk学习 原文:http://blog.chinaunix.net/uid-23302288-id-3785105.html http://www.zsythink.net/archives/t ...
- @enable跟@import注解
参考文章: 讲@import的相关内容:https://blog.csdn.net/u012437781/article/details/78626134 讲为什么registrar没有注入:http ...
- CSS选择器比较:queryselector queryselectorall
官网解释: querySelector() and querySelectorAll() are two JavaScript functions very useful when working w ...
- Value cannot be null. Parameter name: source
下图主要想说.net抛错后的优先级, 错误1是根本原因,排第一位: 错误2里的方法包含了错误1,排第二位: 错误3就是整个Action了. 类似这样的错误,按照这样的顺序来解决bug,相信很受用.
- 浅谈windows.onload()与$(document).ready()
浏览器加载完DOM后,会通过javascript为DOM元素添加事件,在javascript中,通常使用window.onload()方法. 在jquery中,则使用$(document).ready ...
- Debug view 是个好工具
有时候不用 VS 调试, 在 代码里面加入 Debug.Writeline(" Debug information!!"); 这个时候打开 debug view 就可以检测出输出 ...
- python3绘图示例6-1(基于matplotlib,绘图流程介绍及设置等)
#!/usr/bin/env python# -*- coding:utf-8 -*- import os import pylab as pyimport numpy as npfrom matpl ...
- COGS 449. 网络病毒
★★ 输入文件:virus.in 输出文件:virus.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 公元2008年10月31日星期五,笃志者所在的整个机房由 ...
- MATLAB/OCTAVE常用命令 cheat sheet
MATLAB cheatsheet http://web.mit.edu/18.06/www/Spring09/matlab-cheatsheet.pdf 清除变量 clear 清屏 clc //cl ...