[LeetCode]138复制带随机指针的链表
题目描述:
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。 要求返回这个链表的深度拷贝。
思路:
先遍历链表,将每个节点对应的随机指针指向的对象利用HashMap存起来,key的值就为节点的在链表里面的位置,Value的值是随机指针指向的对象
再把原链表的节点对应的在链表中的位置存起来,key为节点对象,然后将节点的label取出来新建节点,存起来
再次遍历链表,处理随机指针
这是第一种用HashMap的办法,但是性能不行,还有优化的空间,之后我再研究研究看能不能改改
public static RandomListNode copyRandomList(RandomListNode head) {
HashMap<Integer,RandomListNode> randomMap = new HashMap<>();//随机节点
HashMap<Integer,RandomListNode> newMap = new HashMap<>();//新的节点
HashMap<RandomListNode,Integer> oldMap = new HashMap<>();//原表的节点
RandomListNode result = head;
if(head==null)
return head;
if(head.next==null){
RandomListNode it = new RandomListNode(head.label);
if (head.random!=null){
it.random = it;//指向自己
}
return it;
}
//迭代
RandomListNode iterator = head;
int i = 0;
while(iterator!=null){
//将第几位对应的随机指针存起来
randomMap.put(i,iterator.random);
//原链表节点对应的位置存起来
oldMap.put(iterator,i);
//新建节点,复制
RandomListNode node = new RandomListNode(iterator.label);
newMap.put(i,node);
if(i==0){
//第一个
result = node;
}
iterator = iterator.next;
i++;
}
i = 0;
iterator = head;
while(iterator!=null){
if(i>0)
newMap.get(i-1).next = newMap.get(i);
//检测原节点的随机指针是否为空
if(oldMap.get(randomMap.get(i))!=null){
//获得原链表节点的随机指针指向的对象的位置
int random = oldMap.get(randomMap.get(i));
//赋值
newMap.get(i).random = newMap.get(random);
}else {
//随机指针为空的情况
newMap.get(i).random = null;
}
iterator = iterator.next;
i++;
}
return result;
}
static class RandomListNode {
int label;
RandomListNode next, random;
RandomListNode(int x) { this.label = x; }
}
第二种办法,先将节点复制,插入到被复制的节点的后面,然后将随机节点加入进去,之后拆分链表
import java.util.*;
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
if (pHead==null)
return null;
RandomListNode current = pHead;
//将复制的节点插入
while (current!=null){
RandomListNode clone = new RandomListNode(current.label);
RandomListNode next = current.next;
current.next = clone;
clone.next = next;
current = next;
}
//将随机节点加入
current = pHead;
while (current!=null){
current.next.random = current.random==null?null:current.random.next;//指向新的随机节点
current = current.next.next;
}
//拆分
current = pHead;
RandomListNode res = pHead.next;
while (current!=null){
RandomListNode clone = current.next;
current.next = clone.next;
clone.next = clone.next==null?null:clone.next.next;
current = current.next;
}
return res;
}
[LeetCode]138复制带随机指针的链表的更多相关文章
- Java实现 LeetCode 138 复制带随机指针的链表
138. 复制带随机指针的链表 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的 深拷贝. 我们用一个由 n 个节点组成的链表来表示输入/ ...
- LeetCode 138——复制带随机指针的链表
1. 题目 2. 解答 第一次遍历链表的时候,复制旧链表的节点值建立一个新的链表,同时定义一个 unordered_map 作为哈希表,哈希表的键为旧链表的节点指针,值为新链表的节点指针. 然后,第二 ...
- Leetcode 138. 复制带随机指针的链表
1.题目要求 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 2.解题思路 (1)笔试思路(求速度,拿分数):使用哈希表 /* ...
- 【leetcode 138. 复制带随机指针的链表】解题报告
方法一:递归 unordered_map<Node*,Node*> dict; Node* copyRandomList(Node* head) { if (!head) return h ...
- LintCode-105.复制带随机指针的链表
复制带随机指针的链表 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点. 返回一个深拷贝的链表. 挑战 可否使用O(1)的空间 标签 哈希表 链表 优步 code / ...
- LeetCode 138:复制带随机指针的链表 Copy List with Random Pointer
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. A linked list is given such that each no ...
- 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- 138 Copy List with Random Pointer 复制带随机指针的链表
给出一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点.返回一个深拷贝的链表. 详见:https://leetcode.com/problems/copy-list- ...
- [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- 2018.12.19 codeforces 1092F. Tree with Maximum Cost(换根dp)
传送门 sbsbsb树形dpdpdp题. 题意简述:给出一棵边权为1的树,允许选任意一个点vvv为根,求∑i=1ndist(i,v)∗ai\sum_{i=1}^ndist(i,v)*a_i∑i=1n ...
- windows socket扩展函数
1.AcceptEx() AcceptEx()用于异步接收连接,可以取得客户程序发送的第一块数据. BOOL AcceptEx( _In_ SOCKET sListenSocket, ...
- Calendar 得到前一天当前时间
@Test public void test(){ //因为Calendar的构造方法是私有的,所以实例化一个Calendar对象用getInstance方法 Calendar calendar = ...
- javascript 经典问题汇总
1. ["1","2","3"].map(parseInt) 为多少?答:[1,NaN,NaN]array.map(function(cur ...
- base_expr +: width_expr
在Verilog-1995中,可以选择向量的任一位输出,也可以选择向量的连续几位输出,不过此时连续几位的始 末数值的index需要是常量.而在Verilog-2001中,可以用变量作为index,进行 ...
- 1.2.2实现Runnable接口
使用Runnable创建线程 package com.cky.runner; /** * Created by chenkaiyang on 2017/12/2. */ public class My ...
- C++之const限定符(顶层const,底层const)
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7050815.html C++之const限定符(顶层const,底层cons ...
- Shell编程-08-Shell中的循环语句
目录 while语句 until语句 for语句 select语句 循环中断控制 循环语句总结 循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行.在Shell中常见的 ...
- CentOS 6 安装Redmine
Redmine是一个灵活的项目管理web应用,采用Ruby on Rails框架开发.Redmine是典型的web 2.0网站,项目管理系统的后起之秀.Redmine支持多项目,灵活的角色权限管理,灵 ...
- Alpha阶段敏捷冲刺(五)
1.站立式会议 提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 祁泽文:实现了个人遗忘曲线图 徐璐琳:完成了微信Web ...