当前位置:网站首页>Sword finger offer 35. replication of complex linked list
Sword finger offer 35. replication of complex linked list
2022-07-28 10:54:00 【jjj34】
Title Description

Knowledge point :
1. Hashtable Relevant knowledge points of hash table _jcxj2934 The blog of -CSDN Blog
2. Related knowledge of linked list : The finger of the sword Offer 06. Print linked list from end to end _jcxj2934 The blog of -CSDN Blog
The idea and code are as follows :
Writing reference leetcode Official answer
/*
// 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 {
HashMap<Node,Node> map = new HashMap<>();
public Node copyRandomList(Node head) {
// Through the first next Create a new linked list <1>
// Give it after you create it random assignment
// do random You can go through hash Table to quickly find the new node corresponding to the old node
//key It is an old linked list node ,value Is a new linked list node
if(head == null)
{
return null;
}
// If the node doesn't exist , create <1>
// stay next It doesn't exist when traversing , So this one if Is to pass next Create all the new linked lists
if(!map.containsKey(head))
{
Node headnew = new Node(head.val);
map.put(head,headnew);
headnew.next = copyRandomList(head.next);
headnew.random = copyRandomList(head.random);
}
// by random Only when you assign a value , Return to new node
return map.get(head);
}
}边栏推荐
猜你喜欢
随机推荐
低代码十问:一文讲透关于低代码的一切!
PyQt5快速开发与实战 4.11 拖曳与剪贴板
GKConstantNoiseSource
Blue Bridge Cup embedded Hal library ADC
nodejs:检测并安装npm模块,如果已安装则跳过
图片滑动特效
GKSpheresNoiseSource
Characteristics and installation of non relational database mongodb
Operation log of dbeaver
c语言进阶篇:指针(一)
OCR knowledge summary
Nodejs:mongodb 简单模糊+分页查询实例
蓝桥杯嵌入式-HAL库-SYSTICK
Pyqt5 rapid development and practice 4.11 drag and clipboard
GKSphereObstacle
剑指 Offer 35. 复杂链表的复制
GKLinearCongruentialRandomSource
GKVoronoiNoiseSource
GKARC4RandomSource
19. Delete the penultimate node of the linked list









