当前位置:网站首页>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);
}
}
边栏推荐
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
- Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
- 第六章 数据流建模—课后习题
- Using HashMap to realize simple cache
- Introduction to memory layout of FVP and Juno platforms
- Haut OJ 1350: choice sends candy
- 二十六、文件系统API(设备在应用间的共享;目录和文件API)
- lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- [merge array] 88 merge two ordered arrays
- SAP method of modifying system table data
猜你喜欢
SAP method of modifying system table data
[speed pointer] 142 circular linked list II
Reader writer model
To the distance we have been looking for -- film review of "flying house journey"
YOLOv5-Shufflenetv2
[to be continued] [UE4 notes] L3 import resources and project migration
浅谈JVM(面试常考)
第六章 数据流建模—课后习题
Merge sort
Bucket sort
随机推荐
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Maximum number of "balloons"
Double pointer Foundation
2022上半年全国教师资格证下
What is the agile proportion of PMP Exam? Dispel doubts
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
服务熔断 Hystrix
Summary of Haut OJ 2021 freshman week
Haut OJ 1401: praise energy
Using HashMap to realize simple cache
Add level control and logger level control of Solon logging plug-in
第六章 数据流建模—课后习题
Haut OJ 1352: string of choice
卷积神经网络——卷积层
Shell Sort
The present is a gift from heaven -- a film review of the journey of the soul
剑指 Offer 05. 替换空格
Yolov5 ajouter un mécanisme d'attention
数仓项目的集群脚本
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution