【原创】leetCodeOj --- Copy List with Random Pointer 解题报告
题目地址:
https://oj.leetcode.com/problems/copy-list-with-random-pointer/
题目内容:
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
/**
* 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) {}
* };
*/
方法:
难点在于random指针需要指向一个复制过程中还不存在的结点。解决办法就简单了,先复制完单链表,再处理每个结点的random指针,这样复制random指针时其指向的结点就已经存在了。
为了高效完成这个工作,我们需要建立一个映射,以旧地址为键,新地址为值,方便复制random指针。
全部代码:
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head)
return NULL;
unordered_map<int,int> dict;
RandomListNode *tmpHead = head;
RandomListNode *resHead = NULL;
RandomListNode *now = NULL;
RandomListNode *nxt = NULL;
resHead = (RandomListNode *)malloc(sizeof(RandomListNode));
dict[(int)head] = (int)resHead;
resHead->label = tmpHead->label;
resHead->next = NULL;
resHead->random = NULL;
now = resHead;
tmpHead = tmpHead->next;
while (tmpHead)
{
nxt = (RandomListNode *)malloc(sizeof(RandomListNode));
nxt->label = tmpHead->label;
nxt->next = NULL;
nxt->random = NULL;
dict[(int)tmpHead] = (int)nxt;
now->next = nxt;
now = nxt;
tmpHead = tmpHead->next;
}
tmpHead = head;
while (tmpHead)
{
now = (RandomListNode *)dict[(int)tmpHead];
if (tmpHead->random != NULL)
now->random = (RandomListNode *)dict[(int)(tmpHead->random)];
tmpHead = tmpHead->next;
}
return resHead;
}
};
【原创】leetCodeOj --- Copy List with Random Pointer 解题报告的更多相关文章
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 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 ...
- 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】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- [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 ...
随机推荐
- 树莓派玩耍笔记4 -- 树莓派ssh党必备的配置
1. 关闭桌面显示 对于ssh 党.当然不须要系统花费资源在显示上. 所以我们先在 "raspi-conifg" 下选择默认启动为Text 启动(这好像也是Raspbian 的默认 ...
- 解决CentOS无法显示中文字体 | 系统运维 | Web2.0
解决CentOS无法显示中文字体 | 系统运维 | Web2.0 About Me 博客园 devops 前端 张家港水蜜桃 傍晚好! 2013年09月12日 17:56:08 ...
- NSDictionary、NSMutableDictionary基本使用
郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...
- birkenfeld / sphinx-contrib — Bitbucket
birkenfeld / sphinx-contrib — Bitbucket README for sphinx-contrib This repository contains a collect ...
- 全面剖析Redis Cluster原理和应用 (转)
1.Redis Cluster总览 1.1 设计原则和初衷 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最核心的目标有三个: 性能:这是Redis赖以生 ...
- 所有javax包
所有jar包 > javax javax 下载 javax jar 包 本站下载镜像: javax-jmi-model.jar.zip javax-jmi-reflect.jar.zip jav ...
- Android 监听SMS短信
当设备接收到一条新的SMS消息时,就会广播一个包括了android.provider.Telephony.SMS_RECEIVED动作的Intent. 注意,这个动作是一个字符串值,SDK 1.0不再 ...
- Kdd Cup 2013 总结2
- RPC分布式处理
RPC(远程过程调用)的应用 接触背景 因为工作上某项目的需要设计一种分布式处理耗时的运算,每个节点然后将运算结果返回给中心服务器,而最初未了解RPC这部分之前我的设计是在每一个RPC服务器上搭建一个 ...
- C#之任务,线程和同步
1 概述 对于所有需要等待 的操作,例 如 ,因 为文件 . 数据库或网络访 问都需要一定 的时间,此 时就可以启 动一个新线程,同时完成其他任务,即使是处理密集型的任务,线程也是有帮助的. 2 Pa ...