当前位置:网站首页>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);
}
边栏推荐
- POJ 3101 Astronomy (Mathematics)
- 4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
- AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
- 一文带你搞懂中断按键驱动程序之poll机制
- ENVI 5.3软件安装包和安装教程
- 【勇敢饭饭,不怕刷题之链表】有序链表的合并
- gpu-admission 源码分析
- WeChat applet, global variables change in one place and the state in other places also changes.
- 【机器学习】浅谈正规方程法&梯度下降
- Codeforces 814 C. An impassioned circulation of affection (dp)
猜你喜欢
The impact of development mode on testing
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
微信小程序提交审核历史版本记录从哪里查看
蔚来-软件开发工程师一面记录
负载均衡原理分析与源码解读
Redis设计与实现
做自媒体月入几万?博主们都在用的几个自媒体工具
单目操作符(含原码反码补码转换)
学长告诉我,大厂MySQL都是通过SSH连接的
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
随机推荐
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
一文带你搞懂中断按键驱动程序之poll机制
3款不同类型的自媒体免费工具,有效提高创作、运营效率
力扣练习——58 验证二叉搜索树
阻塞 非阻塞 poll机制 异步
杭电多校-Loop-(不确定性贪心+线段树)
即时零售业态下如何实现自动做账?
[E-commerce operation] Do you really understand social media marketing (SMM)?
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
gpu-admission 源码分析
Memory problems difficult to locate, it is because you do not use ASAN
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
电脑怎么设置屏幕息屏时间(日常使用分享)
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
1-IMU参数解析以及选择
不止跑路,拯救误操作rm -rf /*的小伙儿
Weilai-software development engineer side record
越折腾越好用的 3 款开源 APP
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
软件架构简介