当前位置:网站首页>剑指 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);
}
}
边栏推荐
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- Double pointer Foundation
- Research on the value of background repeat of background tiling
- Haut OJ 1245: large factorial of CDs --- high precision factorial
- kubeadm系列-00-overview
- [转]MySQL操作实战(三):表联结
- Zzulioj 1673: b: clever characters???
- Generate filled text and pictures
- Shell Sort
- To the distance we have been looking for -- film review of "flying house journey"
猜你喜欢
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
[turn to] MySQL operation practice (I): Keywords & functions
Heap sort summary
sync.Mutex源码解读
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
YOLOv5添加注意力机制
[转]: OSGI规范 深入浅出
Double pointer Foundation
远程升级怕截胡?详解FOTA安全升级
一个新的微型ORM开源框架
随机推荐
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
[转]:Apache Felix Framework配置属性
挂起等待锁 vs 自旋锁(两者的使用场合)
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Haut OJ 1243: simple mathematical problems
Chapter 6 data flow modeling - after class exercises
利用HashMap实现简单缓存
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Haut OJ 1241: League activities of class XXX
High precision subtraction
质量体系建设之路的分分合合
Software test -- 0 sequence
Haut OJ 1350: choice sends candy
Gbase database helps the development of digital finance in the Bay Area
[转]MySQL操作实战(一):关键字 & 函数
What is the agile proportion of PMP Exam? Dispel doubts
Under the national teacher qualification certificate in the first half of 2022
SAP-修改系统表数据的方法
Insert sort
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column