当前位置:网站首页>LeetCode 138. Copy a linked list with random pointers
LeetCode 138. Copy a linked list with random pointers
2022-08-10 11:57:00 【Mizuna pen】
原题网址:https://leetcode.cn/problems/copy-list-with-random-pointer/submissions/
给一个链表,Make a deep copy of the linked list.
使用mapsave copy;Then you can get the current nodenext,random的拷贝节点;Reorganize its relationship;
// Similar to the idea of deep clone graph,mapSave a copy in,
// 两次遍历,The first traversal constitutes the linked list,The second pass is sorting random pointers
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) {
// The node to which the current node is associated
Node next = cur.next;
Node random = cur.random;
// Get the copy node of the associated node;
// in organizing its relationships;
Node copy = map.get(cur);
copy.next = map.get(next);
copy.random = map.get(random);
cur = cur.next;
}
return map.get(head);
}
边栏推荐
- Analysis of the name matching process between the LCD driver and the device (Tiny4412)
- POJ 3101 Astronomy (Mathematics)
- 单目操作符(含原码反码补码转换)
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
- 网络套接字(UDP和TCP编程)
- Spss-多元回归案例实操
- Programmers pursue technology to consolidate basic learning route suggestions
- 皕杰报表在传参乱码
- LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
猜你喜欢
随机推荐
暑期总结4
力扣练习——58 验证二叉搜索树
rider内Mono脚本找不到引用资源
电脑怎么设置屏幕息屏时间(日常使用分享)
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
OSSCore 开源解决方案介绍
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
ENVI 5.3软件安装包和安装教程
【机器学习】浅谈正规方程法&梯度下降
ViT结构详解(附pytorch代码)
【Redis】内存回收策略
LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
模块九 - 设计电商秒杀系统
从脚本到剪辑,影像大师亲授的后期制作秘籍
网络基础(第一节)
不止跑路,拯救误操作rm -rf /*的小伙儿
因为找不到lombok而找不到符号log
接口定义与实现
关于振弦采集模块及采集仪振弦频率值准确率的问题
网络套接字(UDP和TCP编程)








