当前位置:网站首页>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);
}
}
边栏推荐
- YOLOv5-Shufflenetv2
- Palindrome (csp-s-2021-palin) solution
- On-off and on-off of quality system construction
- Haut OJ 1321: mode problem of choice sister
- Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- 二十六、文件系统API(设备在应用间的共享;目录和文件API)
- [转]MySQL操作实战(三):表联结
- Haut OJ 1401: praise energy
- [binary search] 34 Find the first and last positions of elements in a sorted array
猜你喜欢
随机推荐
【ES实战】ES上的native realm安全方式使用
剑指 Offer 58 - II. 左旋转字符串
Little known skills of Task Manager
剑指 Offer 06.从头到尾打印链表
Gbase database helps the development of digital finance in the Bay Area
第六章 数据流建模—课后习题
Web APIs DOM节点
卷积神经网络——卷积层
Quick sort summary
Demonstration of using Solon auth authentication framework (simpler authentication framework)
Insert sort
软件测试 -- 0 序
Bucket sort
sync.Mutex源码解读
Haut OJ 1352: string of choice
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
Drawing dynamic 3D circle with pure C language
High precision subtraction
[merge array] 88 merge two ordered arrays
Pointnet++ learning








