mycode

报错:Node with val 1 was not copied but a reference to the original one.

其实我并没有弄懂对于ListNode而言咋样才叫做深拷贝了

"""
# Definition for a Node.
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
if not head : return None
dummy = p = ListNode(-1)
while head:
p.next = head
p = p.next
head = head.next
return dummy.next

这道链表的深度拷贝题的难点就在于如何处理随机指针的问题,由于每一个节点都有一个随机指针,这个指针可以为空,也可以指向链表的任意一个节点,如果我们在每生成一个新节点给其随机指针赋值时,都要去遍历原链表的话,OJ 上肯定会超时,所以我们可以考虑用 HashMap 来缩短查找时间,第一遍遍历生成所有新节点时同时建立一个原节点和新节点的 HashMap,第二遍给随机指针赋值时,查找时间是常数级。

参考

https://www.cnblogs.com/zuoyuan/p/3745126.html

1、先不考虑random的问题,在原链表上构建copy

2、在copy链表上,利用now.next.random 也需要等于now.random.next实现随即指针的拷贝

3、两个链表分离

"""
# Definition for a Node.
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
if head == None: return None
tmp = head
while tmp:
newNode = Node(tmp.val,None,None)
newNode.next = tmp.next
tmp.next = newNode
tmp = tmp.next.next
tmp = head
while tmp:
if tmp.random:
tmp.next.random = tmp.random.next
tmp = tmp.next.next
newhead = head.next
pold = head
pnew = newhead
while pnew.next:
pold.next = pnew.next
pold = pold.next
pnew.next = pold.next
pnew = pnew.next
pold.next = None
pnew.next = None
return newhead

leetcode-hard-ListNode-Copy List with Random Pointer-NO的更多相关文章

  1. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  2. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

  3. 【LeetCode】138. Copy List with Random Pointer

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

  4. LeetCode OJ: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 复制带随机指针的链表 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...

  6. LeetCode题解之Copy List with Random Pointer

    1.题目描述 2.问题分析 首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可. 3.代码 RandomListNode *copyR ...

  7. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  8. Copy List with Random Pointer leetcode java

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

  9. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  10. 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 ...

随机推荐

  1. springboot 服务端获取前端传过来的参数7种方式

    下面为7种服务端获取前端传过来的参数的方法  1.直接把表单的参数写在Controller相应的方法的形参中,适用于GET 和 POST请求方式 这种方式不会校验请求里是否带参数,即下面的userna ...

  2. 请求上下文HttpContext解释

    1 HttpContext上下文作用 有关应用程序状态信息,处理的请求以及构建的响应等信息全部通过HttpContext上下文获取 2 Httpcontext类用于从头至尾跟踪请求的状态,他也是有关请 ...

  3. C++ DLL导出的两种方式和链接的两种方式

    第一种 导出方式 extern "C" _declspec(dllexport) int Plus(int x, int y); extern "C" _dec ...

  4. 解决maven 引用JDK内部类编译错误 程序包:com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler不存在

    当maven项目里面有用到JDK内部的一些类或者接口的时候,用maven编译一般会出现如下错误: 程序包:com.sun.xml.internal.bind.marshaller.CharacterE ...

  5. 05_ Flume多级Agent之间串联案例

    多级agent之间串联: 从tail命令获取数据发送到avro端口,另一个节点可配置一个avro源来获取数据,发送外部存储 启动两个Agent节点: 使用scp命令拷贝flume安装包到另一台虚拟机; ...

  6. Python Flask学习笔记(1)

    1.搭建虚拟环境 a. 安装 virtualenv : pip3 install virtualenv b. 建立虚拟环境 : 任意目录下建立一个空文件(我的是 Py_WorkSpace) ,在该文件 ...

  7. 5.caffe:train.sh 和 test.sh (训练与测试 )

    一,train.sh #!/usr/bin/env sh ./build/tools/caffe train --solver=myself/00b/solver.prototxt # cd CAFF ...

  8. Linux下安装LoaderRunner LoaderGenerator

    Linux负载机上安装LR的理由: 1.windows系统,基本上每个vuser会消耗2-5M的内存,单机300-500或者更多的虚拟用户时,CPU占用率已经较高,此时整个测试环境的测试系统的瓶颈实际 ...

  9. 【转】golang 结构体和方法

    原文:https://www.jianshu.com/p/b6ae3f85c683 ---------------------------------------------------------- ...

  10. 0912 for循环及内置方法

    目录 for 循环 1.循环 2.循环取值 3.range 4.for + break 5.for+continue 6.for + else 数字类型内置方法 整型 浮点型 字符串 1.作用 2.作 ...