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- ...
随机推荐
- Python之Numpy库常用函数大全(含注释)
前言:最近学习Python,才发现原来python里的各种库才是大头! 于是乎找了学习资料对Numpy库常用的函数进行总结,并带了注释.在这里分享给大家,对于库的学习,还是用到时候再查,没必要死记硬背 ...
- iframe调用父页面js函数 方法 元素
在一个页面中添加iframe,但是有时需要与父页面进行通信,传递参数. 网上总结有以下方法: 一.iframe标签中 src属性传参 <iframe src="test.jsp?i ...
- cerery
cerery http://docs.celeryproject.org/en/latest/userguide/index.html
- Theseven relationsarein threecategories:equivalent, congruent, andsimilar.
http://www.math.pitt.edu/~xfc/math2370/chap5.pdf
- mybatis入门(四)
mybatis入门 需求:根据id查询用户的信息 mysql数据库: CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `us ...
- java的多生产者多消费者例子
import java.util.concurrent.locks.*; public class Test9 { public static void main(String[] args) { / ...
- ubuntu搜狗拼音安装
1.官方下载deb 2.双击安装 3.终端im-config,选择fcitx 4.重启 5.输入法设置中add一下sougoupinyin
- 找到bashrc
(1)直接sudo gedit ~/.bashrc就可以了,编辑完后关闭就行 (2)主文件夹下ctrl+h就能找到.bashrc文件 之所以要找到bashrc文件,是为了把命令 source /opt ...
- socket.io emit 常见用法
io.on('connect', onConnect); function onConnect(socket){ // 只发给sender. sending to the client socket. ...
- php排序方法之选择排序
//选择排序法 $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function selectSort($arr){ for ( $i=0; $i<cou ...