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.

Approach #1: Java.

/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
HashMap<RandomListNode, RandomListNode> visitedHash = new HashMap<RandomListNode, RandomListNode>(); public RandomListNode copyRandomList(RandomListNode head) {
if (head == null)
return null; if (this.visitedHash.containsKey(head))
return this.visitedHash.get(head); RandomListNode node = new RandomListNode(head.label); this.visitedHash.put(head, node); node.next = this.copyRandomList(head.next);
node.random = this.copyRandomList(head.random); return node;
}
}

  

Approach #2: Python.

# Definition for singly-linked list with a random pointer.
# class RandomListNode(object):
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None class Solution(object):
def __init__(self):
self.visited = {} def getClonedNode(self, node):
if node:
if node in self.visited:
return self.visited[node]
else:
self.visited[node] = RandomListNode(node.label)
return self.visited[node]
return None def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
if not head:
return head old_node = head new_node = RandomListNode(old_node.label)
self.visited[old_node] = new_node while old_node != None:
new_node.random = self.getClonedNode(old_node.random)
new_node.next = self.getClonedNode(old_node.next) old_node = old_node.next
new_node = new_node.next return self.visited[head]

  

Analysis: https://leetcode.com/problems/copy-list-with-random-pointer/

At the first I can't understand the mean of this question, after seeing the Solution I understood. However, my basic of Link List is weak, so ^^^

138. Copy List with Random Pointer (not do it by myself)的更多相关文章

  1. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

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

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

  3. leetcode 138. Copy List with Random Pointer ----- java

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

  4. 138. 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】138. Copy List with Random Pointer

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

  6. 138. Copy List with Random Pointer (Graph, Map; DFS)

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

  7. Java for LeetCode 138 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] 138. Copy List with Random Pointer 拷贝带随机指针的链表

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

  9. 138 Copy List with Random Pointer 复制带随机指针的链表

    给出一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点.返回一个深拷贝的链表. 详见:https://leetcode.com/problems/copy-list- ...

随机推荐

  1. 获取一组radio按钮选中的值Value

    1.效果 2.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" ...

  2. 九度OJ 1126:打印极值点下标 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4613 解决:1646 题目描述: 在一个整数数组上,对于下标为i的整数,如果它大于所有它相邻的整数, 或者小于所有它相邻的整数,则称为该整 ...

  3. Java类加载器( 死磕7)

    [正文]Java类加载器(  CLassLoader )死磕7:  基于加密的自定义网络加载器 本小节目录 7.1. 加密传输Server端的源码 7.2. 加密传输Client端的源码 7.3. 使 ...

  4. svn下载

    首先介绍的是SVN安装包的下载,分别包括服务器版和客户端版 下载地址:http://subversion.apache.org/packages.html 打开后,点击Windows 分别下载客户端( ...

  5. Mongoose学习(3)--设置环境变量

    比如我一套代码数据库代码分为中文站和英文站,每个表中我都有一个site_code字段来区分, 两个站点部署在不同的人服务器,这个时候我们就用系统环境变量来区分, 下面直接在mac下设置环境变量 vim ...

  6. xcode7和ios9下UIWebView不能加载网页的解决方法

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  7. Sqoop hive导出到mysql[转]

    通过Sqoop将Hive表数据导入到MySQL通常有两种情况. 第一种是将hive上某张表的全部数据导入到mysql对应的表中. 第二种是将hive上某张表中的部分数据导入到mysql对应的表中. 两 ...

  8. UVA11892 ENimEN —— 博弈

    题目链接:https://vjudge.net/problem/UVA-11892 题意: 两人玩游戏,有n堆石子,每堆有ai块石子,两人轮流取,要求一次只能选择一堆石子取任意块.最后取完的获胜. 题 ...

  9. ffmpeg 编码h264 profile如何设置为baseline的问题

    http://blog.csdn.net/kisaa133/article/details/7792008 使用最新版ffmpeg-0.11 libx264-125,使用默认编码时,用Eyecard发 ...

  10. Java(二)——开发环境搭建 安装JDK和配置环境变量

    1.安装JDK 下载地址  http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下载 ...