当前位置:网站首页>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
边栏推荐
- What is IGMP? What is the difference between IGMP and ICMP?
- QT learning 01 GUI program principle analysis
- 由GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable}引起的刨根问底
- 西门子低代码 9.14版本: 满足不同需求
- Solr basic operation 2
- Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally
- xutils3传集合
- LC:最大子数组和
- Set up enterprise NTP time server
- [wechat applet] understand the basic composition of the applet project
猜你喜欢
基于zfoo开发项目的一些规范
复制带随机指针的链表[空间换时间--hash记录]
QT learning 01 GUI program principle analysis
matlab习题 —— 程序控制流程练习
New titanium cloud service won the "2022 love analysis · panoramic report of it operation and maintenance manufacturers" cloud management platform CMP representative manufacturer
The concept and significance of mean, variance, standard deviation and covariance
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
克隆无向图[bfs访问每条边而不止节点]
QT learning 06 widgets and window types
随机推荐
Digital collection of cultural relics, opening a new way of cultural inheritance
Exploration and Practice on the future direction of byte cloud database
如何实现搜索引擎中的拼写纠错功能——思路
Which securities company should I choose to open an account online? Also, is it safe to open an account online?
[wechat applet] understand the basic composition of the applet project
除子
Xutils3 transfer set
QT learning 02 GUI program example analysis
剑指 Offer 14- II. 剪绳子 II
333333333333333333333333333333
Cacti maximum monitoring number test
雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前
QT learning 07 coordinate system in QT
手机开户后多久才能通过?另外,手机开户安全么?
漫画安全HIDS、EDR、NDR、XDR
LC: effective Sudoku + rotating image
Analysis of define incdir command in TCL script of Modelsim
【微信小程序】认识小程序项目的基本组成结构
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
西门子低代码 9.14版本: 满足不同需求