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. 在mac下搭建Apacheserver

    Apache作为最流行的Webserver端软件之中的一个.它的长处与地位不言而喻.以下介绍下在mac下搭建Apacheserver的步骤: (1)"前往" –>" ...

  2. RequestDispatcher用法

    1.具体用法: RequestDispatcher dispatcher = req.getRequestDispatcher("updateByUserId_001.jsp"); ...

  3. mongodb学习之:数据库

    首先来介绍下Mongodb的基本概念: 左边一列是关系数据库的术语,右边这一列是NOSQL也就是mongodb的术语 database:       database         数据库 tabl ...

  4. iOS 打开应用与系统功能的调用

    [[UIApplication sharedApplication] openURL:url]; 通过给url不同的值,可以实现调用系统自带 电话/短信/邮箱/浏览器/...   1.调用 电话pho ...

  5. POJ1061 青蛙的约会 —— 扩展gcd

    题目链接:https://vjudge.net/problem/POJ-1061 青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  6. FFmpeg滤镜代码级分析

    http://blog.chinaunix.net/uid-26000296-id-3322071.html 前一篇文章<为FFmpeg添加自定义滤镜>详细讲述了FFmpeg的滤镜添加步骤 ...

  7. RequireJS 配置理解

    RequireJS 配置: 1.首先加载RequireJS文件 <script src="//cdn.bootcss.com/require.js/2.1.22/require.js& ...

  8. Python: PS 滤镜--水波特效

    本文用 Python 实现 PS 滤镜中的 水波特效 import numpy as np from skimage import img_as_float import matplotlib.pyp ...

  9. CentOS6.3安装 Oracle 11g R2

    1.设置系统1.1.包需求 11g很人性化了,增加了检查的机制,还有就是不象10g那样在64位下要安装很多的32位包,只需要安装一个gcc-32bit-4.3就OK了.下列表必须要安装:binutil ...

  10. windows查看与清理dns缓存

    1.windows下在命令行输入  ipconfig /flushdns 清理本地dns缓存查看dns缓存   ipconfig/displaydns2.Mac OSX下在命令行输入  lookupd ...