当前位置:网站首页>剑指 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);
}
}
边栏推荐
猜你喜欢
![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[转]: OSGI规范 深入浅出

Romance of programmers on Valentine's Day

Web APIs DOM node

Talking about JVM (frequent interview)

支持多模多态 GBase 8c数据库持续创新重磅升级

Introduction to tools in TF-A

浅谈JVM(面试常考)

Collapse of adjacent vertical outer margins

十年不用一次的JVM调用

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
随机推荐
Software test -- 0 sequence
PMP考试敏捷占比有多少?解疑
Use of room database
YOLOv5添加注意力机制
被舆论盯上的蔚来,何时再次“起高楼”?
Yolov5 adds attention mechanism
Simple modal box
Download xftp7 and xshell7 (official website)
Add level control and logger level control of Solon logging plug-in
Reflection summary of Haut OJ freshmen on Wednesday
Use of snippets in vscode (code template)
Es module and commonjs learning notes -- ESM and CJS used in nodejs
Bucket sort
How can the Solon framework easily obtain the response time of each request?
Solon Auth 认证框架使用演示(更简单的认证框架)
对象的序列化
质量体系建设之路的分分合合
SAP method of modifying system table data
Haut OJ 1401: praise energy
[trans]: spécification osgi