当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
单目操作符(含原码反码补码转换)
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
使用哈工大LTP测试分词并且增加自定义字典
gpu-admission 源码分析
常量及数据类型你还记得多少?
3款不同类型的自媒体免费工具,有效提高创作、运营效率
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
Article take you understand interrupt the key driver of polling mechanism
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
The brave rice rice, does not fear the brush list of 】 list has a ring
随机推荐
Since the media hot style title how to write?Taught you how to write the title
接口定义与实现
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
SQL优化最强总结 (建议收藏~)
使用.NET简单实现一个Redis的高性能克隆版(六)
彩色图和深度图转点云
【LeetCode】640. 求解方程
HDU 4135:Co-prime (容斥原理)
Stroke Practice - 62 Valid Sudokus
不止跑路,拯救误操作rm -rf /*的小伙儿
【Untitled】
How many constants and data types do you remember?
LeetCode 92. 反转链表 II
学长告诉我,大厂MySQL都是通过SSH连接的
HDU 4135: Co-prime (the principle of inclusion and exclusion)
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
如何使用工程仪器设备在线监测管理系统
关于振弦采集模块及采集仪振弦频率值准确率的问题