当前位置:网站首页>Force button list question
Force button list question
2022-07-26 09:18:00 【Xiao Tang Xuejie】
Give you a length of n The linked list of , Each node contains an additional random pointer random , The pointer can point to any node or null node in the list .
To construct this linked list Deep copy . The deep copy should be made exactly by n individual new Node composition , The value of each new node is set to the value of its corresponding original node . New nodes next Pointers and random The pointer in the list should also point to the new node , And make these pointers in the original linked list and the copied linked list represent the same linked list state . The pointer in the copy linked list should not point to the node in the original linked list .
for example , If there is... In the original linked list X and Y Two nodes , among X.random --> Y . Then the corresponding two nodes in the copy linked list x and y , There are also x.random --> y .
Returns the header node of the copy linked list .
With a n The input is represented by a list of nodes / The linked list in the output . Each node uses one [val, random_index] Express :
val: A said Node.val The integer of .
random_index: The index of the node that the random pointer points to ( Range from 0 To n-1); If you don't point to any nodes , Then for null .
Your code only Accept the header node of the original linked list head As an input parameter .
Example 1:
Input :head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output :[[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
Input :head = [[1,1],[2,1]]
Output :[[1,1],[2,1]]
Example 3:
Input :head = [[3,null],[3,0],[3,null]]
Output :[[3,null],[3,0],[3,null]]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/copy-list-with-random-pointer
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
/*
// 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;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
// 1. First traverse the old linked list , Create a corresponding new node for each old node , And insert it into Map in
Map<Node,Node> map = new HashMap<>();
for(Node cur = head; cur != null; cur = cur.next) {
map.put(cur,new Node(cur.val));
}
// 2. Traverse the old linked list again , structure next/ random The direction of
for (Node oldNode = head; oldNode != null; oldNode = oldNode.next) {
// hold next Want to connect back
Node newNode = map.get(oldNode);
Node newNodeNext = map.get(oldNode.next);
newNode.next = newNodeNext;
// hold random Connect with each other
Node newNodeRandom = map.get(oldNode.random);
newNode.random = newNodeRandom;
}
return map.get(head);
}
}
边栏推荐
- 220. Presence of repeating element III
- mysql函数
- Matlab 绘制阴影误差图
- [MySQL] detailed explanation of MySQL lock (III)
- [MySQL] how to execute an SQL statement (2)
- Cat安装和使用
- Server memory failure prediction can actually do this!
- JS - DataTables control on the number of displays per page
- 【ARKit、RealityKit】把图片转为3D模型
- Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
猜你喜欢

What is the difference between NFT and digital collections?

十大蓝筹NFT近半年数据横向对比

Horizontal comparison of the data of the top ten blue chip NFTs in the past half year

NFT与数字藏品到底有何区别?

209. Subarray with the smallest length

Cat安装和使用

Redis principle and usage - installation and distributed configuration

ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知

Li Mu D2L (V) -- multilayer perceptron

Polynomial open root
随机推荐
Error: Cannot find module ‘umi‘ 问题处理
220. Presence of repeating element III
Grain College of all learning source code
Local cache
Elastic APM installation and use
网络安全漫山遍野的高大上名词之后的攻防策略本质
【final关键字的使用】
【Mysql】Mysql锁详解(三)
围棋智能机器人阿法狗,阿尔法狗机器人围棋
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
PHP 之 Apple生成和验证令牌
高数 | 武爷『经典系列』每日一题思路及易错点总结
Qtcreator reports an error: you need to set an executable in the custom run configuration
Datax的学习笔记
Zipkin installation and use
Mysql事务
839. 模拟堆
js闭包:函数和其词法环境的绑定
【Mysql】一条SQL语句是怎么执行的(二)
[MySQL] how to execute an SQL statement (2)