当前位置:网站首页>剑指 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);
}
}
边栏推荐
- 小程序直播+電商,想做新零售電商就用它吧!
- Add level control and logger level control of Solon logging plug-in
- 一个新的微型ORM开源框架
- SAP method of modifying system table data
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- 小程序直播+电商,想做新零售电商就用它吧!
- 软件测试 -- 0 序
- Add level control and logger level control of Solon logging plug-in
- [turn]: Apache Felix framework configuration properties
猜你喜欢
随机推荐
[speed pointer] 142 circular linked list II
kubeadm系列-02-kubelet的配置和启动
用STM32点个灯
被舆论盯上的蔚来,何时再次“起高楼”?
Service fusing hystrix
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
PMP candidates, please check the precautions for PMP examination in July
High precision subtraction
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
Haut OJ 1243: simple mathematical problems
sync. Interpretation of mutex source code
[turn]: OSGi specification in simple terms
Heap sort summary
Haut OJ 1357: lunch question (I) -- high precision multiplication
第六章 数据流建模—课后习题
Haut OJ 1350: choice sends candy
kubeadm系列-01-preflight究竟有多少check
记录QT内存泄漏的一种问题和解决方案
Summary of Haut OJ 2021 freshman week
Haut OJ 1347: addition of choice -- high progress addition