当前位置:网站首页>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);
}
}
边栏推荐
- redis原理和使用-安装和分布式配置
- 2022 mobile crane driver test question simulation test question bank simulation test platform operation
- "No input file specified" problem handling
- CF1481C Fence Painting
- 839. 模拟堆
- [MySQL] detailed explanation of redo log, undo log and binlog (4)
- 2022 chemical automation control instrument operation certificate test question simulation test platform operation
- JS - DataTables 关于每页显示数的控制
- 网络安全漫山遍野的高大上名词之后的攻防策略本质
- C# Serialport的发送和接收
猜你喜欢

Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown

2022 chemical automation control instrument operation certificate test question simulation test platform operation

【Mysql】Mysql锁详解(三)

Cat安装和使用

Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world

CF1481C Fence Painting

Voice chat app source code - Nath live broadcast system source code

The essence of attack and defense strategy behind the noun of network security

Li Mu D2L (V) -- multilayer perceptron
![[online problem] timeout waiting for connection from pool problem troubleshooting](/img/f0/7e8444ed7d0921b98d5e998e274bc8.png)
[online problem] timeout waiting for connection from pool problem troubleshooting
随机推荐
"No input file specified" problem handling
pycharm 打开多个项目的两种小技巧
性格测试系统v1.0
[arkit, realitykit] turn pictures into 3D models
756. 蛇形矩阵
2022 mobile crane driver test question simulation test question bank simulation test platform operation
JS - DataTables control on the number of displays per page
Qtcreator reports an error: you need to set an executable in the custom run configuration
对象型的集合按某个属性的值进行去重
Datax的学习笔记
220. Presence of repeating element III
力扣——二叉树剪枝
异常处理机制二
js闭包:函数和其词法环境的绑定
“could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32” 问题处理
PAT 甲级 A1034 Head of a Gang
The essence of attack and defense strategy behind the noun of network security
tornado之多进程服务
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
NTT(快速数论变换)多项式求逆 一千五百字解析