当前位置:网站首页>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);
}
边栏推荐
- 从源码角度分析UUID的实现原理
- LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
- 力扣练习——60 二叉搜索子树的最大键值和
- 皕杰报表在传参乱码
- 力扣练习——64 最长和谐子序列
- 软件架构简介
- If someone asks you about distributed transactions again, throw this to him
- Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
- 力扣练习——59 从二叉搜索树到更大和树
- 建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
猜你喜欢
随机推荐
Network sockets (UDP and TCP programming)
即时零售业态下如何实现自动做账?
越折腾越好用的 3 款开源 APP
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
HDU 6040 Hints of sd0061 (技巧)
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Buckle Exercise - 61 Sort by frequency of characters
LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
力扣练习——61 根据字符出现频率排序
力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
Spss-多元回归案例实操
Stroke Practice - 62 Valid Sudokus
A case of violent parameter tuning in machine learning
机器学习之暴力调参案例
StoneDB 文档捉虫活动第一季
什么是幂等性?四种接口幂等性方案详解!
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细








