leetcode — copy-list-with-random-pointer
import java.util.*;
/**
*
* Source : https://oj.leetcode.com/problems/copy-list-with-random-pointer/
*
* 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.
*/
public class CopyListWithRandomPointer {
/**
* 拷贝一个链表,链表的每个节点都有一个随即指针,指向链表中任意一个元素或者是null
*
* 建立一个hashmap,将已经复制过的节点放入map中
* 遍历链表,如果该节点的下一个节点cur -> next 已经在hashmap中,表示被复制过,直接连接在next上,否则新建一个节点连接
* 如果该节点的random节点已经在hashmap中,则直接连接到random上,否则新建一个
*
* @param head
* @return
*/
public RandomLinkedList copy (RandomLinkedList head) {
if (head == null) {
return null;
}
Map<RandomLinkedList, RandomLinkedList> map = new HashMap<RandomLinkedList, RandomLinkedList>();
RandomLinkedList p = head;
RandomLinkedList cloneNode = new RandomLinkedList(head.value);
map.put(head, cloneNode);
while (p != null) {
if (p.next != null) {
if (map.containsKey(p.next)) {
cloneNode.next = map.get(p.next);
} else {
RandomLinkedList newNode = new RandomLinkedList(p.next.value);
map.put(p.next, newNode);
cloneNode.next = newNode;
}
}
if (p.random != null) {
if (map.containsKey(p.random)) {
cloneNode.random = map.get(p.random);
} else {
RandomLinkedList newNode = new RandomLinkedList(p.random.value);
map.put(p.random, newNode);
cloneNode.next = newNode;}
}
p = p.next;
cloneNode = cloneNode.next;
}
return map.get(head);
}
private static RandomLinkedList createList (int[] arr) {
if (arr.length == 0) {
return null;
}
RandomLinkedList head = new RandomLinkedList(arr[0]);
RandomLinkedList last = head;
List<RandomLinkedList> list = new ArrayList<RandomLinkedList>(arr.length);
list.add(last);
for (int i = 1; i < arr.length; i++) {
RandomLinkedList node = new RandomLinkedList(arr[i]);
last.next = node;
last = node;
list.add(node);
}
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
int rand = Math.abs(random.nextInt()) % (arr.length+1);
if (rand != arr.length) {
list.get(i).random = list.get(rand);
}
}
return head;
}
private static void print (RandomLinkedList head) {
List<Integer> values = new ArrayList<Integer>();
List<String> list = new ArrayList<String>();
while (head != null) {
values.add(head.value);
if (head.random != null) {
list.add(head.value + " ---> " + head.random.value);
}
head = head.next;
}
list.add(0, Arrays.toString(values.toArray(new Integer[values.size()])));
System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
System.out.println();
}
private static class RandomLinkedList {
int value;
RandomLinkedList random;
RandomLinkedList next;
public RandomLinkedList(int value) {
this.value = value;
}
}
public static void main(String[] args) {
CopyListWithRandomPointer copyListWithRandomList = new CopyListWithRandomPointer();
int[] arr = new int[]{1,2,3};
RandomLinkedList head = createList(arr);
print(head);
print(copyListWithRandomList.copy(head));
}
}
leetcode — copy-list-with-random-pointer的更多相关文章
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- [leetcode]Copy List with Random Pointer @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- LeetCode——Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode – Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
随机推荐
- PhpStorm连接服务器,开始自动上传功能
连接服务器 菜单栏找到[工具/Tools]->[Deployment/部署]->[Confinguration-/配置-]. 点加号(+),添加一台服务器,填写名称,选择类型为SFTP,点 ...
- python3.6 urllib.request库实现简单的网络爬虫、下载图片
#更新日志:#0418 爬取页面商品URL#0421 更新 添加爬取下载页面图片功能#0423 更新 添加发送邮件功能# 优化 爬虫异常处理.错误页面及空页面处理# 优化 爬虫关键字黑名单.白名单,提 ...
- day6、Linux下如何找出7天以前的文件删除
有些时候,由于系统产生的日志文件,使服务器的磁盘空间紧张,所以怎么删除7天以前的日志文件及让系统只保留7天以内的日志文件 方法一 使用命令:find + |xargs + ls 命令方法:find / ...
- API Gateway - KONG 安装与配置
简介 Kong,是由Mashape公司开源的,基于Nginx的API gateway 特点 可扩展,支持分布式 模块化 功能:授权.日志.ip限制.限流.api 统计分析(存在商业插件Galileo等 ...
- javaweb学习总结(七)——HttpServletResponse对象(一)(转)
转载自 http://www.cnblogs.com/xdp-gacl/p/3789624.html Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对 ...
- Spring 面试
1.什么是Spring框架?Spring框架有哪些主要模块? spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...
- Android - shape圆形画法(oval)
shape圆形画法(oval) 本文地址: http://blog.csdn.net/caroline_wendy 1. 创建一个目录drawable, 用于存放xml类型的图片资源; 2. 在dra ...
- POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38980 Acc ...
- break and continue
public class HelloWorld { public static void main(String[] args) { int sum = 0; for (int i = 1; i &l ...
- JAVA入门[17]-ControllerAdvice处理exception
1.关于@ControllerAdvice @ControllerAdvice注解本身已经使用了@Component,因此@ControllerAdvice注解所标注的类将会自动被组件扫描获取到,就像 ...