当前位置:网站首页>LeetCode 138. 复制带随机指针的链表
LeetCode 138. 复制带随机指针的链表
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/copy-list-with-random-pointer/submissions/
给一个链表,对该链表进行深拷贝。
使用map保存拷贝;那么就可以得当前节点next,random的拷贝节点;再组织其关系;
// 和深克隆图的思路相似,map中保存副本,
// 两次遍历,第一次遍历组成链表,第二次遍历在整理随机指针
public Node copyRandomList(Node head) {
Map<Node,Node> map = new HashMap<>();
Node cur = head;
while(cur != null) {
map.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
while(cur != null) {
// 当前节点关联的节点
Node next = cur.next;
Node random = cur.random;
// 得到关联节点的拷贝节点;
// 在组织其关系;
Node copy = map.get(cur);
copy.next = map.get(next);
copy.random = map.get(random);
cur = cur.next;
}
return map.get(head);
}
边栏推荐
猜你喜欢
随机推荐
Buckle Exercise - 61 Sort by frequency of characters
Redis常用命令
POJ 3101 Astronomy (数学)
常量及数据类型你还记得多少?
阻塞 非阻塞 poll机制 异步
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
Get started quickly and conquer three different distributed architecture calling schemes
从脚本到剪辑,影像大师亲授的后期制作秘籍
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
POJ 1026 Cipher (置换群)
Codeforces 814 C. An impassioned circulation of affection (dp)
从产品角度看 L2 应用:为什么说这是一个游乐场?
Pulling drills - 56 Finding the right interval
OSSCore 开源解决方案介绍
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
Centos7环境使用Mysql离线安装包安装Mysql5.7
10 个 Reduce 常用“奇技淫巧”
Chapter 22 Source Code File REST API Reference (4)









