Java for LeetCode 138 Copy List with Random Pointer
2024-10-19 18:28:26
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
解题思路:
我们在Java for LeetCode 133 Clone Graph题中做过图的复制,本题和图的复制十分类似,JAVA实现如下:
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null)
return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode node = new RandomListNode(head.label);
RandomListNode headTemp = head, nodeTemp = node;
map.put(head, node);
while (headTemp.next != null) {
nodeTemp.next = new RandomListNode(headTemp.next.label);
map.put(headTemp.next, nodeTemp.next);
headTemp = headTemp.next;
nodeTemp = nodeTemp.next;
}
headTemp = head;
nodeTemp = node;
while (headTemp!= null) {
if(map.containsKey(headTemp.random))
nodeTemp.random=map.get(headTemp.random);
headTemp = headTemp.next;
nodeTemp = nodeTemp.next;
}
return node;
}
最新文章
- Unity依赖注入使用
- Hadoop.2.x_时间服务器搭建(CentOs6.6)
- winform最小化后隐藏到右下角,单击或双击后恢复 .
- <;math.h>;与<;float.h>;
- 图片上传并显示(兼容ie),图片大小判断
- Mysql大小写敏感的问题 --转
- 文件操作IO流
- (五)认识Android中的Service
- JS左侧菜单-03
- 完善chrome翻译插件ChaZD,支持有道智云api
- 洛谷P2221 [HAOI2012]高速公路
- SQL Server 连接 MySQL
- Android打包遇到的那些坑
- Oracle启用scott用户
- Android中本地广播的实现
- 使用 Navicate 连接 Oracle9i 数据库
- 比较全git的.ignore文件配置
- maven 错误列表
- JAVA-Servlet高级应用
- IDEA安装以及项目初始化
热门文章
- linux系统故障分析与排查
- SparkStreaming和Drools结合的HelloWord版
- js 快速排序
- 升级Jekyll 3.0
- vue.js+koa2项目实战(二)创建 HeadBar 组件
- 【Python】继承
- Xutils的get请求后,总是返回同样数据的问题解决方式
- Chrome 插件 CrxMouse 去除后门优化版
- VueJS自定义过滤器:new Vue({filters:{filter1:function(){}....}})
- C语言重要概念汇总