当前位置:网站首页>剑指 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規範 深入淺出
- [paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
- [interval problem] 435 Non overlapping interval
- [turn to] MySQL operation practice (III): table connection
- Talking about JVM (frequent interview)
- Quick sort summary
- Pointnet++的改进
- JVM call not used once in ten years
- [turn to] MySQL operation practice (I): Keywords & functions
- Add level control and logger level control of Solon logging plug-in
猜你喜欢

On-off and on-off of quality system construction

对象的序列化

Improvement of pointnet++

Gbase database helps the development of digital finance in the Bay Area

Research on the value of background repeat of background tiling

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](/img/b1/8b843014f365b786e310718f669043.png)
[turn to] MySQL operation practice (I): Keywords & functions
![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)
[interval problem] 435 Non overlapping interval

Page countdown

远程升级怕截胡?详解FOTA安全升级
随机推荐
Acwing 4301. Truncated sequence
On-off and on-off of quality system construction
使用Electron开发桌面应用
object serialization
远程升级怕截胡?详解FOTA安全升级
[binary search] 34 Find the first and last positions of elements in a sorted array
Under the national teacher qualification certificate in the first half of 2022
Page countdown
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
[转]:Apache Felix Framework配置属性
sync. Interpretation of mutex source code
Research on the value of background repeat of background tiling
Haut OJ 1357: lunch question (I) -- high precision multiplication
使用命令符关闭笔记本自带键盘命令
Introduction to tools in TF-A
Haut OJ 1241: League activities of class XXX
浅谈JVM(面试常考)
Pointnet++学习
Software test -- 0 sequence
Zzulioj 1673: b: clever characters???