LeetCode题解之Copy List with Random Pointer
1、题目描述

2、问题分析
首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可。
3、代码
RandomListNode *copyRandomList(RandomListNode *head) {
if( head == NULL){
return NULL;
}
RandomListNode* newhead = new RandomListNode();
RandomListNode* np = newhead;
RandomListNode* p = head;
map<RandomListNode*, RandomListNode*> m;
while (p != NULL){
RandomListNode* tmp = new RandomListNode(p->label);
m.insert(make_pair(p,tmp));
np->next = tmp;
np = np->next;
p = p->next;
}
p = head ;
np = newhead->next;
while( p != NULL){
np->random = m[p->random];
p = p->next;
np = np->next;
}
return newhead->next;
}
LeetCode题解之Copy List with Random Pointer的更多相关文章
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 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 ...
随机推荐
- JavaScript -- Table-方法
-----049-Table-方法.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&quo ...
- Jquery初体验一
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- windows平台,实现录音功能详解
音频处理分为播放和录音两类.对这些处理,微软提供了一些列函数,称之为Waveform Functions.这篇文章讨论录音功能.会对微软提供的函数做简单说明,并对这些函数封装成c++类,再进一步封装成 ...
- Ansible-安装-秘钥-部署-使用
本文转自:https://www.cnblogs.com/ylqh/p/5902259.html ansiblemaster:192.168.74.146 ansibleslave1 :192.168 ...
- Mybatis插件使用-统计sql执行时间
背景介绍:最近由于产品数据量较大,sql执行十分低效,正在做数据库优化,所以想在日志中看到每个sql执行的时间,以方便针对性的优化. 查找相关资料,了解到Mybatis有一款插件,是基于interce ...
- springboot+cloud 学习(一)高可用服务注册中心(Eureka)
先说说Eureka Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringClo ...
- windows系统 PHP安装Redis扩展
cmd开启/关闭redis redis所在路径: D:\redis\Redis-x64-3.2.100 启动: start cmd /k "cd /d D:\redis\Redis-x64- ...
- MongoDB的“not master and slaveok=false”错误解决
在客户端操作MongoDB时经常会如下错误: SECONDARY> show collections; Fri Jul :: uncaught exception: error: { } 原因是 ...
- UIKit 框架之UIActionSheet
UIAlertView和UIActionSheet相似,区别很小, 很容易理解. // // ViewController.m // UIActionSheet // // Created by Ci ...
- SQLServer 里面的 DDL,DML,DCL,TCL
1.DDL (Data Definition Language )数据库定义语言 statements are used to define the database structure or sch ...