138. Copy List with Random Pointer (not do it by myself)
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)的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 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 ...
- 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 ...
- [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 ...
- 138 Copy List with Random Pointer 复制带随机指针的链表
给出一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点.返回一个深拷贝的链表. 详见:https://leetcode.com/problems/copy-list- ...
随机推荐
- 基于UDP的一对回射客户/服务器程序
前言 之前曾经学习过一对回射客户/服务器程序的例子,不过那个是基于TCP协议的.本文将讲解另一对回射客户/服务器程序,该程序基于UDP协议.由于使用的协议不同,因此编写出的程序也有本质上的区别,应将它 ...
- Apache/2.4.9启动错误:AH01630: client denied by server configuration
在升级Yii框架1.11->2.0beta时,PHP升级到5.5.顺带升级Apache2.2.x到2.4.9. 把原有vhost配置移植过来,出现Apache启动错误: AH01630: cli ...
- shift:解决shell编程中的入渗问题
我说过了,shell是我的常规武器,目前虽然还不纯熟,但是我爱shell这门语言,在Linux下面混,总要写脚本.程序员是有基因,对编程语言是有偏好的,你让我写C代码,我会觉得很爽,会有困难,会有痛苦 ...
- iOS UI13_数据解析XML_,JSON
- (IBAction)parserButton:(id)sender { parserXML *parser =[[parserXML alloc] init]; [parser startPars ...
- hdu1427 速算24点
</pre><pre> //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #in ...
- Machine Learning in Action(7) 回归算法
按照<机器学习实战>的主线,结束有监督学习中关于分类的机器学习方法,进入回归部分.所谓回归就是数据进行曲线拟合,回归一般用来做预测,涵盖线性回归(经典最小二乘法).局部加权线性回归.岭回归 ...
- appium(9)-uiautomator UiSelector
uiautomator UiSelector Appium enables searching using UiSelectors. UiScrollable is also supported.// ...
- appium(6)-parts of appium api
parts of appium api Lock Lock the screen.//锁屏. // java driver.lockScreen(3); // objective c [driver ...
- leetcode 748. Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- alsa 编程
ALSA(Advanced Linux Sound Architecture)是由内核驱动,标准的API库和一系列实用程序组成.因为涉及到版权和BUG的问题Linux 2.6内核抛弃了旧的OSS,AL ...