当前位置:网站首页>Replication of complex linked list
Replication of complex linked list
2022-07-21 17:23:00 【ZJH'blog】
Please implement copyRandomList function , Copy a complex list . In complex linked list , Each node has one next The pointer points to the next node , One more random The pointer points to any node in the list or null.
/*
// 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;
}
}
*/
/**
Law 1: use hashMap To maintain the
*/
class Solution {
public Node copyRandomList(Node head) {
if(head == null)
return null;
HashMap<Node,Node> hs = new HashMap<Node,Node>();
Node curr = head;
while(curr != null){
hs.put(curr,new Node(curr.val));
curr = curr.next;
}
curr = head;
while(curr != null){
hs.get(curr).next = hs.get(curr.next);
hs.get(curr).random = hs.get(curr.random);
curr = curr.next;
}
return hs.get(head);
}
}
边栏推荐
- 12、用户微服务
- 华为无线设备配置同一业务VLAN的AP间快速漫游
- [MySQL] simple installation in CentOS 7
- I used redis distributed lock, grabbed bottle Maotai, and then GG~~
- 泄漏电流、无接地保护措施……1296个进口电热水壶存在重大安全隐患
- JS -- loop -- guess numbers (generate random numbers with decimal math.random)
- 有了线程后,发生的变化
- [email protected]交互
- 16 SQL injection test points you should pay attention to
- Detailed explanation of JUC concurrent programming wait notify
猜你喜欢
进程的组织方式:链接方式和索引方式
【图文并茂】这次一文讲透JVM架构、类文件结构、字节码结构!!
如何分析并设计性能测试场景
[Interview: concurrence19: Multithreading: Park & unpark]
Power Bi ---- slicer to make the report more beautiful
Time series analysis of port berths during Arima epidemic
Huawei wireless device configuration WMM and priority mapping
Four redis cluster schemes and their advantages and disadvantages
如何用度量数据驱动代码评审的改善
进程的五种状态
随机推荐
有没有完全自主的国产化数据库技术
4种 Redis 集群方案及优缺点对比
竣达技术丨MOBUS 转SNMP网络监控终端
OpenCV学习——ArUco模块
如何将服务器上的文件_按自定义的层级关系_进行压缩并下载
Work experience summary 1
Cmake Quick Guide
Acme Automation - free SSL certificate application and automatic renewal
JS--循环--猜数字(生成随机数存在小数Math.random)
IT运维管理指什么?如何建立有效的IT运维管理系统?
华为无线设备配置不同业务VLAN的AP间非快速漫游
线程间有哪些资源是不可以共享的?
MD编辑器 - Typora
"Cloud primitives kubernetes" deeply analyzes the principle of pod in kubernetes
卷积神经网络单图超分辨率的深度学习方法
进程的五种状态
第06篇:池化技术
Four redis cluster schemes and their advantages and disadvantages
复杂链表的复制
[solution] VC6.0 cannot be debugged at breakpoints