当前位置:网站首页>Leetcode-138-copy linked list with random pointer
Leetcode-138-copy linked list with random pointer
2022-07-26 19:30:00 【z754916067】
subject

Ideas
- No idea , Copy the linked list to copy , however random The node position is not easy to record , Even if it is recorded, it must be traversed every time , If exactly all nodes point to the last node, it is O(n^2) Time complexity of .
- Have you thought about the solution of the problem , But at that time, I wanted to hash the node values of the linked list , But the node value will repeat , So it got stuck , Originally, you can store nodes directly , Solve with recursion … Learned .
Code
// Building hash tables It is used to correspond to the original node and the current node
Map<Node,Node> cacheNode = new HashMap<Node,Node>();
public Node copyRandomList(Node head) {
// This is the end point of recursion
if(head==null) return null;
if(!cacheNode.containsKey(head)){
// If the hash table key Not included head That is, the node has not been created
// With head Create a new node
Node headNew = new Node(head.val);
cacheNode.put(head,headNew);
head.next = copyRandomList(head.next); // Recursion is completed by default Already copy Out of its next node
head.random = copyRandomList(head.random);// The principle of same
}
// If it has been included , Just return to the new node
return cacheNode.get(head);
}
边栏推荐
猜你喜欢

第九章 实用建模技术

C#创建及读取DAT文件案例

千万不要随便把 Request 传递到异步线程里面 , 有坑 你拿捏不住,得用 startAsync 方法才行

香港高防IP优势及哪些行业适合使用

(ICLR-2022)TADA!用于视频理解的时间自适应卷积

Conda+pytorch environment tutorial

How to solve the problem that win11 has been switched on after upgrading

MapReduce(二)

MapReduce (II)
![[swoole series 3.1] have you been asked about processes, threads, and collaborations during the interview?](/img/62/2aa1999f461ea5afd19b78bcd4ded8.jpg)
[swoole series 3.1] have you been asked about processes, threads, and collaborations during the interview?
随机推荐
配置服务器环境
[yolov5] - detailed version of training your own dataset, nanny level learning, logging, hand-in-hand tutorial
Sentinel isolation and degradation
查看容器的几种方式
中信建投启牛会员优惠开户安全吗,不知道是不是最低的佣金
密码一致,总显示如下图
If the key is forgotten and multiple devices have different keys, how does the cloud synchronize
Wechat applet plug-in -- wxml to canvas (generate pictures)
ReentrantLock学习之---基本属性
TypeScript阶段学习
“蔚来杯“2022牛客暑期多校训练营1
Last blog post
File depth monitoring strategy
LeetCode简单题之数组能形成多少数对
时空预测4-graph wavenet
Machine learning notes - building a recommendation system (6) six automatic encoders for collaborative filtering
香港高防IP优势及哪些行业适合使用
Introduce the difference between @getmapping and @postmapping in detail
ReentrantLock学习之---释放锁过程
【MySQL必知必会】 日志Log详解