当前位置:网站首页>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);
}
边栏推荐
- 三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
- Article take you understand interrupt the key driver of polling mechanism
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- rider内Mono脚本找不到引用资源
- LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
- 【Redis】内存回收策略
- 微信小程序提交审核历史版本记录从哪里查看
- 建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
- Buckle Exercise - 61 Sort by frequency of characters
- 使用哈工大LTP测试分词并且增加自定义字典
猜你喜欢

2022年裁员潮,失业程序员何去何从?

L2 applications from a product perspective: why is it a playground?

从脚本到剪辑,影像大师亲授的后期制作秘籍

Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了

ENVI 5.3软件安装包和安装教程

Nocalhost - 让云原生时代的开发更高效

基于UiAutomator2+PageObject模式开展APP自动化测试实战

APP automation testing practice based on UiAutomator2+PageObject mode

常量及数据类型你还记得多少?

Since the media hot style title how to write?Taught you how to write the title
随机推荐
力扣练习——58 验证二叉搜索树
皕杰报表在传参乱码
老板加薪!看我做的WPF Loading!!!
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
LeetCode 24. 两两交换链表中的节点
Programmers pursue technology to consolidate basic learning route suggestions
ENVI 5.3软件安装包和安装教程
被面试官问到消息队列的丢失、重复与积压问题该如何回答
项目部署、
If someone asks you about distributed transactions again, throw this to him
[E-commerce operation] Do you really understand social media marketing (SMM)?
LeetCode 25. K 个一组翻转链表
Not just running away, but saving the guy who mishandled rm -rf /*
10 个 Reduce 常用“奇技淫巧”
再有人问你分布式事务,把这篇扔给他
APP automation testing practice based on UiAutomator2+PageObject mode
Network Fundamentals (Section 1)
负载均衡原理分析与源码解读
3款不同类型的自媒体免费工具,有效提高创作、运营效率
What are some useful performance testing tools recommended? Performance testing report charging standards