当前位置:网站首页>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);
}
边栏推荐
- 杭电多校-Loop-(不确定性贪心+线段树)
- [Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
- 怎么加入自媒体,了解这5种变现模式,让账号快速变现
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
- 即时零售业态下如何实现自动做账?
- 单目操作符(含原码反码补码转换)
- The impact of development mode on testing
- Get started quickly and conquer three different distributed architecture calling schemes
- Weilai-software development engineer side record
- 微信小程序提交审核历史版本记录从哪里查看
猜你喜欢
电脑怎么设置屏幕息屏时间(日常使用分享)
The brave rice rice, does not fear the brush list of 】 list has a ring
中小规模网站架构
L2 applications from a product perspective: why is it a playground?
微信小程序提交审核历史版本记录从哪里查看
Short video software development - how to break the platform homogenization
第2章-矩阵及其运算-矩阵运算(2)
rider内Mono脚本找不到引用资源
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
随机推荐
A little self-deprecating deconstruction about farmers "code"
使用JMeter进行MySQL的压力测试
Programmers pursue technology to consolidate basic learning route suggestions
The impact of development mode on testing
自媒体爆款标题怎么写?手把手教你写热门标题
Since the media hot style title how to write?Taught you how to write the title
[Go WebSocket] 多房间的聊天室(一)思考篇
2022年裁员潮,失业程序员何去何从?
From the product dimension, why can't we fully trust Layer2?
皕杰报表在传参乱码
Chapter 22 Source Code File REST API Reference (4)
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
暑期总结4
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
Nocalhost - Making development more efficient in the cloud-native era
Break through the dimensional barriers and let the dolls around you move on the screen!
Introduction to Software Architecture
从产品维度来看 我们为什么不能完全信任Layer2?
从源码角度分析UUID的实现原理
Get started quickly and conquer three different distributed architecture calling schemes