leetcode-hard-ListNode-Copy List with Random Pointer-NO
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的更多相关文章
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 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 ...
- 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- LeetCode题解之Copy List with Random Pointer
1.题目描述 2.问题分析 首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可. 3.代码 RandomListNode *copyR ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 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 ...
随机推荐
- footer始终在页面最底部的方法(问题待检验)
一.css方法 <style type="text/css"> html,body{ height: 100%; } body{ display: flex; flex ...
- 【Struts2】防止表单重复提交
一.概述 二.Struts2中解决方案 三.实现步骤 一.概述 regist.jsp----->RegistServlet 表单重复提交 危害: 刷票. 重复注册.带来服务器访问压力(拒绝服务) ...
- 四、DML语言
目录 简介 主要操作 插入语句 语法 修改语句 修改单表 删除语句 DELETE TRUNCATE 两种删除总结 简介 DML语言就是数据操作语言 主要操作 插入:insert 修改:update 删 ...
- zencart单独屏蔽左右边栏代码
1.屏蔽左栏:打开模板目录下的tpl_main_page.php文件,找到以下代码 if (!isset($flag_disable_left) || !$flag_disable_left) { 在 ...
- Windows&Appium&Python自动化测试-Appium安装
一.安装node.js 官方下载地址为:https://nodejs.org/en/download 傻瓜式安装即可,安装完成后,CMD中运行node -v查看版本号 输入npm 出现如上图信息,表示 ...
- 一个 TCP 连接可以发多少个 HTTP 请求?
曾经有这么一道经典面试题:从 URL 在浏览器被被输入到页面展现的过程中发生了什么?相信大多数准备过的同学都能回答出来,但是如果继续问:收到的 HTML 如果包含几十个图片标签,这些图片是以什么方式. ...
- linux基础_用户和组的三个文件
1./etc/passwd文件 用户(user)的配置文件,记录用户的各种信息 每行的含义:用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录shell 2./etc/shadow文件 口令 ...
- loj515 「LibreOJ β Round #2」贪心只能过样例[bitset+bool背包]
由于bitset极其不熟练且在实际题目中想不起来运用它来优化,于是练了几道题. 这题是一个分组的bool背包,每组必须选一个,暴力的话是$O(n^5)$. 如果dp数组不要一维滚动的话,有两种枚举方法 ...
- list去重的四种方式
L=[1,2,3,3,5,5,5,8,4,6,9,7,2,'a','s','a','e','s','z'] def DelDupli(L): L1=[] for i in L: ...
- C#+Entity Frame work+MVC+Mysql+Apicloud共享汽车管理系统【论文】+Apicloud开发实例
摘要: 共享汽车管理系统主要分为后台管理PC端和手机App端,后台管理可以对指定停车点.车辆基本信息.用户注册信息.用户订单信息.推送消息进行管理和维护,而手机app用户可以通过手机号进行短信注册,根 ...