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. CentOS LNMP环境搭建 各版本

    我们先下载系统包. 以下centos6.5 X64系统 进行演示.本环境适应Centos5.x CentOs6.x Centos7.x    32和64版本.如有错误请回复本文主要安装代码汇总 [PH ...

  2. 网页设计——5.table布局

    今天做一个大的页面,主要是对table布局的理解: 代码: <table cellspacing=0 border=1 style="bordercolor:#C0C0C0;" ...

  3. jar包冲突与inode

    包冲突 几乎上点规模的java系统就会遇到jar冲突,不负责任的讲排除依赖成了每次发布上线前必做的工作.虽然问题的本质都是jar冲突,但是表现上却有很多不同,从NoSuchMethodError,Cl ...

  4. JavaScript中对日期格式化的新想法.

    其实我们对与日期的显示,也就那么几种,不需要每次都传格式化字符串. 只要告诉函数你想要什么结果就好了,以下是在ios的JavaScript中我新写的日期格式化函数: /** 格式化日期 @param ...

  5. json篇

    QQ:1187362408 欢迎技术交流和学习 json篇(json): TODO: 1,json:json是什么( JSON(JavaScript Object Notation) 是一种轻量级的数 ...

  6. c语言中字符串函数的使用

    #include<stdio.h> #include<string.h> /* char s1[]="I am a student"; char s2[20 ...

  7. bmp文件格式中rgb555与rgb888之间的转换,24位与16位位图的转换

    今日,有同事问我.rgb555模式下的位图文件的格式问题,于是花了一下午的时间通过推測与測试,分析出了例如以下bmp文件在rgb555模式下的文件存储规律: 1:bmp文件的文件信息头中的biBitC ...

  8. Launcher知识的demo,手机管家小伙家与悬浮窗

    Launcher知识的demo.主要仿的手机管家小伙家与悬浮窗的效果.东西比較简单.直接贴代码 主要用到了windowManager 中的更新updateViewLayout(view,params) ...

  9. android之获取屏幕的宽度和高度

    获取屏幕的宽度和高度: 方法一: //获取屏幕的宽度 public static int getScreenWidth(Context context) { WindowManager manager ...

  10. Codeforces548A:Mike and Fax

    While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There wer ...