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- ...
随机推荐
- centos7 简单搭建lnmp环境
1:查看环境: 1 2 [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 1 [r ...
- C++雾中风景4:多态引出的困惑,对象的拷贝?
C++作为一门面向对象的语言,自然具备了面向对象的三大特征:封装,继承,多态.在学习多态性质的过程中,发现了C++与其他语言很大的区别(坑?).在C++中的=操作符的使用与C++呈现的内存模型似乎并不 ...
- C#中的Explicit和Implicit
今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault( ...
- java傻瓜简单100%一定看的懂新手安装教程
1.java官网 最新的不是很稳定 http://www.oracle.com/technetwork/java/javase/downloads/index.html 一直点下一步就可以,但别忘 ...
- 》》jqurey mobile 初
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- PHP去除unicode续:json_encode之后,仅仅有文字,数字不见了的解决方法
接前文.http://blog.csdn.net/yanzi1225627/article/details/44985487 这么处理了一段时间.确实没发现问题.但近期发现了一个bug.比方输入&qu ...
- Android中使用ContentProvider进行跨进程方法调用
原文同一时候发表在我的博客 点我进入还能看到很多其它 需求背景 近期接到这样一个需求,须要和别的 App 进行联动交互,比方下载器 App 和桌面 App 进行联动.桌面的 App 能直接显示下载器 ...
- 单行json_ajax
html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- android 事件传递机制(1)
在项目中,经常遇到事件冲突,ScrollView,ViewPager滑动卡顿等情况,比如:onClick和onLongClick事件冲突,dispatchTouchEvent,onInterceptT ...
- Python爬虫(二十四)_selenium案例:执行javascript脚本
本章叫介绍如何使用selenium在浏览器中使用js脚本,更多内容请参考:Python学习指南 隐藏百度图片 #-*- coding:utf-8 -*- #本篇将模拟执行javascript语句 fr ...