当前位置:网站首页>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);
}
}
边栏推荐
- Solon 框架如何方便获取每个请求的响应时间?
- Pointnet++ learning
- 剑指 Offer 35.复杂链表的复制
- The number of enclaves
- sync. Interpretation of mutex source code
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- FVP和Juno平台的Memory Layout介绍
- [depth first search] 695 Maximum area of the island
- Acwing 4301. Truncated sequence
- YOLOv5添加注意力機制
猜你喜欢

游戏商城毕业设计
![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[转]: OSGI规范 深入浅出

Reader writer model

Introduction to tools in TF-A

Solution to the palindrome string (Luogu p5041 haoi2009)

Quick sort summary

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

Heap sort summary

lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

剑指 Offer 09. 用两个栈实现队列
随机推荐
Bucket sort
Development error notes
利用HashMap实现简单缓存
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
Haut OJ 1245: large factorial of CDs --- high precision factorial
Binary search basis
Pointnet++ learning
Haut OJ 1241: League activities of class XXX
剑指 Offer 58 - II. 左旋转字符串
质量体系建设之路的分分合合
Demonstration of using Solon auth authentication framework (simpler authentication framework)
The number of enclaves
挂起等待锁 vs 自旋锁(两者的使用场合)
Introduction to tools in TF-A
Merge sort
YOLOv5-Shufflenetv2
剑指 Offer 53 - I. 在排序数组中查找数字 I
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Haut OJ 1401: praise energy
剑指 Offer 04. 二维数组中的查找