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的更多相关文章

  1. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  3. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  4. LeetCode——Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  7. [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. LeetCode – Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  10. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

随机推荐

  1. SQL基础教程读书笔记-1

    查询基础 2.2 算数运算符和比较运算符 2.2.1算数运算符 + - * / 需要注意NULL 5 + NULL 10 - NULL 1 * NULL 4 / NULL NULL / 9 NULL ...

  2. Common Data Service (CDS) 初探

    作者:陈希章 发表于 2017年12月16日 前言 Common Data Service(以下简称为CDS),通用数据服务是一个创新性的基础功能,这是微软试图打造一个全新的基于SaaS模式的数据服务 ...

  3. Redis的五种数据类型及方法

    字符串string: 字符串类型是Redis中最为基础的数据存储类型,是一个由字节组成的序列,他在Redis中是二进制安全的,这便意味着该类型可以接受任何格式的数据,如JPEG图像数据货Json对象描 ...

  4. 自动化运维工具——puppet详解(二)

    一.class 类 1)什么是类? 类是puppet中命名的代码模块,常用于定义一组通用目标的资源,可在puppet全局调用: 类可以被继承,也可以包含子类: 具体定义的语法如下: class NAM ...

  5. (转)java中的方法区

    首先要说明的是,此文章转载自 http://blog.csdn.net/zzhangxiaoyun/article/details/7518917 谢谢作者.是本人认为对方法区解释得比较清楚的一篇文章 ...

  6. 【Jenkins】通过ANT构建JMeter任务时提示找不到jtl文件时的解决方法

  7. 【java】反射简单示例

    package 反射; public class Test反射 { public static void main(String[] args) { System.out.println(Runtim ...

  8. iOS转场动画封装

    写在前面 iOS在modal 或push等操作时有默认的转场动画,但有时候我们又需要特定的转场动画效果,从iOS7开始,苹果就提供了自定义转场的API,模态推送present和dismiss.导航控制 ...

  9. less基础语法

    变量 //->LESS代码 @link-color: #428bca; @link-color-hover: darken(@link-color, 10%); a { color: @link ...

  10. IOS学习8——常用框架学习汇总

    我们在学习和code过程中经常会用到一些框架,本文将会持续更新最新学习和用到的框架 布局框架: Masonry介绍与使用实践:快速上手Autolayout iOS MJRefresh下拉.上拉刷新自定 ...