当前位置:网站首页>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);
}
}
边栏推荐
- Mysql事务
- ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
- STM32+MFRC522完成IC卡号读取、密码修改、数据读写
- Simple message mechanism of unity
- 839. 模拟堆
- Sending and receiving of C serialport
- NTT(快速数论变换)多项式求逆 一千五百字解析
- JS - DataTables 关于每页显示数的控制
- 优秀的 Verilog/FPGA开源项目介绍(三十零)- 暴力破解MD5
- Introduction to excellent verilog/fpga open source project (30) - brute force MD5
猜你喜欢
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
[MySQL] how to execute an SQL statement (2)
Innovus is stuck, prompting x error:
Redis principle and use - Basic Features
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
Server memory failure prediction can actually do this!
深度学习常用激活函数总结
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
多项式开根
volatile 靠的是MESI协议解决可见性问题?(下)
随机推荐
对标注文件夹进行清洗
Pat grade a a1013 battle over cities
滑动窗口、双指针、单调队列、单调栈
CF1481C Fence Painting
838. Heap sorting
Redis principle and use - Basic Features
Polynomial open root
SQL入门——组合表
Simple message mechanism of unity
MySQL 强化知识点
pycharm 打开多个项目的两种小技巧
[MySQL] how to execute an SQL statement (2)
Matlab 绘制阴影误差图
839. 模拟堆
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
2022年上海市安全员C证考试试题及模拟考试
Qtcreator reports an error: you need to set an executable in the custom run configuration
谷粒学院的全部学习源码
论文笔记: 知识图谱 KGAT (未完暂存)