leetcode 【 Copy List with Random Pointer 】 python 实现
题目:
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.
代码:Runtime: 215 ms
# Definition for singly-linked list with a random pointer.
# class RandomListNode:
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None class Solution:
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
if head is None:
return head # insert newnode between every two nodes between oldlist
p = head
while p is not None:
newnode = RandomListNode(p.label)
tmp = p.next
p.next = newnode
newnode.next = tmp
p = tmp # copy random point
p = head
while p is not None:
if p.random is not None:
p.next.random = p.random.next
p = p.next.next # extract the new list from mixed list
newhead = head.next
p = head
while p is not None:
tmp = p.next
p.next = p.next.next
p = p.next
if tmp.next:
tmp.next = tmp.next.next
tmp = tmp.next return newhead
思路:
自己想不出来巧的方法 网上找个靠谱的帖子:
参照上述帖子的思路写的python代码。
遇到的一个问题是,一开始判断极端case的时候有“if head.next is None: return head”
结果一直报错,后来去掉后AC了。注意一个点的时候也要复制。
还有就是,一直对python里面变量间的赋值不太清楚,google了一篇如下的日志,讲的比较靠谱一些。
http://www.cnblogs.com/evening/archive/2012/04/11/2442788.html
leetcode 【 Copy List with Random Pointer 】 python 实现的更多相关文章
- [leetcode]Copy List with Random Pointer @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- LeetCode——Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode – Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
随机推荐
- LaTeX 符号大全
常用数学符号的 LaTeX 表示方法 2016-10-31 16:22 | 黄荣生 常用数学符号的 LaTeX 表示方法 1.指数和下标可以用^和_后加相应字符来实现.比如: 2.平方根(squa ...
- Cypress测试工具
参考博客: https://testerhome.com/articles/19035 最近一段时间学习了cypress的测试工具, 她是一个端到端的测试web工具. 环境准备 1.工具:vs co ...
- UML中类图(Class Diagram)的关系整理
什么是UML类图? 类图显示了一组类.接口.协作以及他们之间的关系.在UML中问题域最终要被逐步转化,通过类来建模,通过编程语言构建这些类从而实现系统.类加上他们之间的关系就构成了类图,类图中还可以包 ...
- 定时器new Timer().schedule()的使用
Timer是一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行.实际上是个线程,定时调度所拥有的TimerTasks. TimerTask是一个抽象类,它的子类由 ...
- linux 命令——5 rm(转)
昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所 ...
- java中的两同两小一大原则
子类覆盖父类要遵循“两同两小一大” “两同”即方法名相同,形参列表相同 “两小”指的是子类方法返回值类型应比父类方法返回值类型更小或相等,子类方法声明抛出的异常类应比父类方法声明抛出的异常类更小或相等 ...
- java Html&JavaScript面试题:HTML 的 form 提交之前如何验证数值文本框的内容全部为数字? 否则的话提示用户并终止提交?
提交的验证方法(通过单个字符比较): <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- rem适配方案
页面布局单位计算 一般有两大类:绝对长度单位和相对长度单位 绝对长度单位: px 像素:是显示屏上显示的每一个小点,为显示的最小单位 in 英寸,1in = 96px cm 厘米,1cm = 37.8 ...
- 牛客小白月赛5 A 无关(relationship) 【容斥原理】【数据范围处理】
题目链接:https://www.nowcoder.com/acm/contest/135/A 题目描述 若一个集合A内所有的元素都不是正整数N的因数,则称N与集合A无关. 给出一个含有k个元素的 ...
- 谈谈Integer中的静态类IntegerCache
学习的本质就是一个赋值的过程,用新知识来覆盖你的旧知识或者无知(null).掌握知识是自己的, 分享知识,才能帮助更多的人,创造更大的价值.学贵以恒,以此自勉,与君共享.----曦阳X ...