当前位置:网站首页>Copy linked list with random pointer [space for time --hash record]
Copy linked list with random pointer [space for time --hash record]
2022-06-30 00:01:00 【REN_ Linsen】
Preface
Space for time is a very common way to reduce time complexity , and hashMap/hash Array / Binary system hash Is the usual way .
One 、 Copy list with random pointer

Two 、hash Record
package everyday.hash;
import java.util.HashMap;
import java.util.Map;
public class CopyRandomList {
public Node copyRandomList(Node head) {
// Set up dummy node , Take the special case head==null, As well as the head node problem .
Node dummyHead = new Node(1);
dummyHead.next = head;
Node cloneDummyHead = new Node(1);
// Record the original node and the newly generated node mapping .
Map<Node, Node> fx = new HashMap<>();
// Set the head node and the walk node .
Node p = dummyHead.next;
Node cur = cloneDummyHead.next;
// Set up next
while (p != null) {
// Generate new nodes
Node next = new Node(p.val);
// Record old and new node mappings , Set... For the back random Prepare the pointer .
fx.put(p, next);
// Set new linked list next
cur.next = next;
// to update cur/p
cur = cur.next;
p = p.next;
}
// Set up random
p = dummyHead.next;
cur = cloneDummyHead.next;
while (p != null) {
// obtain cur node random Point to .
Node random = fx.get(p.random);
// Set up random
cur.random = random;
// to update cur/p
cur = cur.next;
p = p.next;
}
// Return the cloned linked list
return cloneDummyHead.next;
}
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
}
summary
1) Space for time ,hashMap/hashSet/hash Array / Binary system hash
reference
边栏推荐
- Basic operations such as MySQL startup under Windows platform
- QT learning 05 introduction to QT creator project
- Create an API rapid development platform, awesome!
- Set up security groups, record domain names, and apply for SSL certificates
- How to write controller layer code gracefully?
- Cartoon security HIDS, EDR, NDR, XDR
- AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
- AI empowers new retail, the way to win "wisdom" lies in ecological thinking | selected excerpts from digital intelligence night talk live broadcast
- AI chief architect 9- huxiaoguang, propeller model library and industry application
- Xutils3 transfer set
猜你喜欢

这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
solo 博客皮肤导入 skins 文件夹后出现 500 错误

Why is JSX syntax so popular?

QT learning 01 GUI program principle analysis

Halcon practical: design idea of solder joint detection

机器学习:VC维的概念和用途

漫画安全HIDS、EDR、NDR、XDR

西门子低代码平台通过Database Connector 连接Mysql 实现增删改查

【微信小程序】认识小程序项目的基本组成结构

QT learning 07 coordinate system in QT
随机推荐
@Scheduled annotation pit, I stepped on it for you
gyctf_2020_document
6.29 problem solving
HPE launched ARM CPU general server product
Xutils3 transfer set
The concept and significance of mean, variance, standard deviation and covariance
Et la tarte aux framboises 4? Quels sont les jeux possibles?
设置安全组、域名备案、申请ssl证书
Set up security groups, record domain names, and apply for SSL certificates
Leetcode (680) -- verifying palindrome string II
Cacti settings for spin polling
手机开户一般哪个证券公司好?另外,手机开户安全么?
QT learning 06 widgets and window types
Buffer flow exercise
shell-位置参数变量和预定义变量
除子
vlog常用参数解析
History of deep learning
MySQL functions and constraints
【一起上水硕系列】Day 8