当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
SQL优化最强总结 (建议收藏~)
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
The impact of development mode on testing
振弦传感器及核心VM系列振弦采集模块
std::move()
机器学习之暴力调参案例
L2 applications from a product perspective: why is it a playground?
随机推荐
2022年裁员潮,失业程序员何去何从?
什么是幂等性?四种接口幂等性方案详解!
Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
从脚本到剪辑,影像大师亲授的后期制作秘籍
Chapter 22 Source Code File REST API Reference (4)
怎么加入自媒体,了解这5种变现模式,让账号快速变现
Where can I view the version record of WeChat applet submission review history?
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
不止跑路,拯救误操作rm -rf /*的小伙儿
Centos7环境使用Mysql离线安装包安装Mysql5.7
3款不同类型的自媒体免费工具,有效提高创作、运营效率
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
快速上手,征服三种不同分布式架构调用方案
使用.NET简单实现一个Redis的高性能克隆版(六)
阻塞 非阻塞 poll机制 异步
基于UiAutomator2+PageObject模式开展APP自动化测试实战
使用.NET简单实现一个Redis的高性能克隆版(六)
A little self-deprecating deconstruction about farmers "code"
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了