当前位置:网站首页>剑指 Offer 35. 复杂链表的复制
剑指 Offer 35. 复杂链表的复制
2022-07-28 10:28:00 【jjj34】
题目描述

知识点:
1.哈希表 哈希表的相关知识点_jcxj2934的博客-CSDN博客
2.链表的相关知识: 剑指 Offer 06. 从尾到头打印链表_jcxj2934的博客-CSDN博客
思路及代码如下:
写法参考了leetcode的官方答案
/*
// 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) {
//先通过next将新链表创建一遍<1>
//创建好后再给random赋值
//做random的时候可以通过hash表来快速找到旧节点对应的新节点
//key 是旧的链表节点,value是新的链表节点
if(head == null)
{
return null;
}
//如果节点不存在,就创建<1>
//在next遍历的时候都是不存在的,所以这一个if是先通过next将新链表都创建一遍
if(!map.containsKey(head))
{
Node headnew = new Node(head.val);
map.put(head,headnew);
headnew.next = copyRandomList(head.next);
headnew.random = copyRandomList(head.random);
}
//为random赋值时才会走到这里,返回新的节点
return map.get(head);
}
}边栏推荐
- GKPolygonObstacle
- Install Office customization. Troubleshooting during installation
- An example of SQL trace in MySQL
- 10_ UE4 advanced_ Add fall and cast actions
- GKRidgedNoiseSource
- 网络文件系统服务(NFS)
- Network file system service (NFS)
- ACM winter vacation training 4
- Codeforces Round #614 (Div. 2) B. JOE is on TV!
- GKPerlinNoiseSource
猜你喜欢
随机推荐
Codeforces Round #614 (Div. 2) B. JOE is on TV!
GKARC4RandomSource
7. MapReduce custom sorting implementation
Excel word simple skills sorting (continuous update ~)
7、MapReduce自定义排序实现
网络文件系统服务(NFS)
GKCoherentNoiseSource
andorid 开发
Attention attention mechanism flow chart
Machine learning -- handwritten English alphabet 3 -- engineering features
GKCoherentNoiseSource
GKRandom
蓝桥杯嵌入式-HAL库-LCD
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
11_ UE4 advanced_ Change male characters to female characters and modify the animation
ACM winter vacation training 5
判断数码管是共阳极还是共阴极
GKNoiseSource
2019年9月PAT甲级题目
OCR 知识 概括









![Implement a queue with two stacks [C language]](/img/8a/679575bb0a562eff7e4317e64b4790.png)