当前位置:网站首页>Sword finger offer 35 Replication of complex linked list
Sword finger offer 35 Replication of complex linked list
2022-07-05 05:28:00 【ThE wAlkIng D】
Title Description

Problem analysis ( Use HashMap To do it )
1. Judge whether it is empty first , If it is empty, return null
2. newly build map Set to copy the original list to the new list
3. Create a pointer to the new linked list
Code instance
/* // 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);
}
}
边栏推荐
- Time complexity and space complexity
- [turn to] MySQL operation practice (III): table connection
- YOLOv5-Shufflenetv2
- object serialization
- Add level control and logger level control of Solon logging plug-in
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- Insert sort
- Heap sort summary
- Haut OJ 1321: mode problem of choice sister
- Reader writer model
猜你喜欢

Pointnet++ learning

Palindrome (csp-s-2021-palin) solution

剑指 Offer 09. 用两个栈实现队列
![[to be continued] [UE4 notes] L3 import resources and project migration](/img/81/6f75f8fbe60e037b45db2037d87bcf.jpg)
[to be continued] [UE4 notes] L3 import resources and project migration

剑指 Offer 53 - I. 在排序数组中查找数字 I
![[turn]: OSGi specification in simple terms](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[turn]: OSGi specification in simple terms

Little known skills of Task Manager

远程升级怕截胡?详解FOTA安全升级

On-off and on-off of quality system construction

SAP-修改系统表数据的方法
随机推荐
Little known skills of Task Manager
用STM32点个灯
过拟合与正则化
读者写者模型
Double pointer Foundation
Acwing 4300. Two operations
[轉]: OSGI規範 深入淺出
SSH password free login settings and use scripts to SSH login and execute instructions
Haut OJ 2021 freshmen week II reflection summary
Mysql database (I)
The present is a gift from heaven -- a film review of the journey of the soul
Count sort
Shell Sort
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
[转]MySQL操作实战(一):关键字 & 函数
注解与反射
剑指 Offer 53 - I. 在排序数组中查找数字 I
PMP candidates, please check the precautions for PMP examination in July
Pointnet++的改进
room数据库的使用