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- ...
随机推荐
- oracle 控制文件多路复用
网上有很多关于控制文件的操作,我大概看了下.有很多都是炒来炒去转来转去.下面以自己理解和操作为例来对oracle的控制文件进行下介绍. 首先介绍下控制文件 在oralce数据库中,控制文件是一个很小的 ...
- EasyDarwin开源流媒体云平台VS调试断点提示“还没有为该文档加载任何符号”的解决办法
本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52063666 近日,我们EasyDarwin ...
- tomcat部署web应用的4种方法以及部署多个应用
原文: tomcat部署web应用的4种方法 在Tomcat中有四种部署Web应用的方式,简要的概括分别是: (1)利用Tomcat自动部署 (2)利用控制台进行部署 (3)增加自定义的Web部署文件 ...
- 6.5.1.3 Caching SHA-2 Pluggable Authentication
MySQL :: MySQL 8.0 Reference Manual :: 6.5.1.3 Caching SHA-2 Pluggable Authentication https://dev.my ...
- Theseven relationsarein threecategories:equivalent, congruent, andsimilar.
http://www.math.pitt.edu/~xfc/math2370/chap5.pdf
- 增删改查,连接数据库UsersDao
package com.abc.dao; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Re ...
- 20170313 ABAP以jason 格式返回值到http(接口内容返回)
问题1: 返回jason 格式信息给你们这步不通, 这个可以怎么处理, ***得到SCP 系统开发回复,他们需要调整方法: (1)调用函数做RETURN, IT_ZSMLSCPNOTICE-FUNC ...
- ubuntu搜狗拼音安装
1.官方下载deb 2.双击安装 3.终端im-config,选择fcitx 4.重启 5.输入法设置中add一下sougoupinyin
- VVDocument+Appledoc生成文档
在写代码的时候写上适当的注释是一种良好的习惯,方便自己或者别人阅读的方便. **VVDocument**:(Github地址:[VVDocument](https://github.com/onevc ...
- CSS中float与A标签的疑问
<stype> a{ text-decoration:none; float:left;} </stype> <div class="box1"> ...