当前位置:网站首页>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);
}
边栏推荐
- conda+pytorch环境教程
- 用低代码搭建千人食品制造企业高效管理系统案例分析
- 时空预测5-GAT
- 节省50%成本 京东云发布新一代混合CDN产品
- Network protocol: tcp/ip protocol
- Gongfu developer community is settled! On July 30!
- Distributed transaction Seata
- This article explains in detail the five benefits that MES system brings to enterprises, with application scenarios
- torch. Usage and comparison of unsqueeze() squeeze() expand() repeat()
- EN 1504-6混凝土结构保护和修理用产品钢筋锚固—CE认证
猜你喜欢

The diagram of user login verification process is well written!

After the exam on June 25, see how the new exam outline reviews PMP

MySQL log introduction

测试面试题集-UI自动化测试

篇7:exited on DESKTOP-DFF5KIK with error code -1073741511.

J3:Redis主从复制

The first letter of leetcode simple question appears twice

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘

C语言-入门-语法-字符串(十一)

Turn off win10 automatic update completely
随机推荐
Sre person in charge of station B personally describes the multi activity disaster recovery construction after the 713 accident | takintalks share
Network protocol: tcp/ip protocol
B站SRE负责人亲述 713事故后的多活容灾建设|TakinTalks大咖分享
如何保护电子商务网站免受网络攻击?
彻底关闭win10自动更新
MapReduce (II)
The difference between advanced anti DDoS server and advanced anti DDoS IP
NLP 学习之路
关于接口测试你想知道的都在这儿了
LeetCode简单题之第一个出现两次的字母
Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available
ReentrantLock学习之---释放锁过程
当前占位,之后再写
C # create and read dat file cases
指标和标签是做什么的
MapReduce(二)
Leetcode simple question: the minimum total time required to fill a cup
LeetCode笔记:Weekly Contest 303
安全测试与功能测试、渗透测试你知道有什么区别吗?
Synchronized theory