当前位置:网站首页>剑指 Offer 35.复杂链表的复制
剑指 Offer 35.复杂链表的复制
2022-07-05 05:26:00 【ThE wAlkIng D】
题目描述

问题解析(使用HashMap来做)
1.先判断是否为空,如果为空则返回null
2.新建map集合来进行原始链表到新链表的复制
3.建立新链表的指针指向
代码实例
/* // 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) {
if(head == null){
return null;
}
Node cur = head;
Map<Node,Node> map = new HashMap<Node,Node>();
while(cur != null){
Map.put(cur,new Node(cur.val));
cur = cur.next;
}
cur = head;
while(cur != null){
Map.get(cur).next = Map.get(cur.next);
Map.get(cur).random = Map.get(cur.random);
cur = cur.next;
}
return Map.get(head);
}
}
边栏推荐
- [turn]: OSGi specification in simple terms
- Reader writer model
- [binary search] 34 Find the first and last positions of elements in a sorted array
- Add level control and logger level control of Solon logging plug-in
- Solon Auth 认证框架使用演示(更简单的认证框架)
- 2022年上半年国家教师资格证考试
- 使用命令符关闭笔记本自带键盘命令
- Bucket sort
- Pointnet++的改进
- Web APIs DOM节点
猜你喜欢

游戏商城毕业设计

Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!

SAP method of modifying system table data

Generate filled text and pictures

2022年上半年国家教师资格证考试

服务熔断 Hystrix

Heap sort summary
![[转]MySQL操作实战(一):关键字 & 函数](/img/b1/8b843014f365b786e310718f669043.png)
[转]MySQL操作实战(一):关键字 & 函数

支持多模多态 GBase 8c数据库持续创新重磅升级
随机推荐
远程升级怕截胡?详解FOTA安全升级
[allocation problem] 135 Distribute candy
Solon 框架如何方便获取每个请求的响应时间?
Yolov5 adds attention mechanism
Haut OJ 1218: maximum continuous sub segment sum
Improvement of pointnet++
Service fusing hystrix
Add level control and logger level control of Solon logging plug-in
Optimization scheme of win10 virtual machine cluster
注解与反射
[interval problem] 435 Non overlapping interval
Drawing dynamic 3D circle with pure C language
Haut OJ 1352: string of choice
[es practice] use the native realm security mode on es
Haut OJ 1316: sister choice buys candy III
High precision subtraction
Transport connection management of TCP
Haut OJ 2021 freshmen week II reflection summary
Solon Auth 认证框架使用演示(更简单的认证框架)
二十六、文件系统API(设备在应用间的共享;目录和文件API)