题目

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

思路

自己想不出来巧的方法 网上找个靠谱的帖子:

http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5ODIzNDQ3Mw==&appmsgid=10000291&itemidx=1&sign=ccde63918a24dee181f1fd1a4e3e6781

参照上述帖子的思路写的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 实现的更多相关文章

  1. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  2. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  3. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  4. LeetCode——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——Copy List with Random Pointer(带random引用的单链表深拷贝)

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

  6. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  7. [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. LeetCode – Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  10. [Leetcode Week17]Copy List with Random Pointer

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

随机推荐

  1. wechat开发笔记之1.接口示例代码

    修改后的php示例代码! <?php /** * wechat php test */ //define your token define("TOKEN", "w ...

  2. 使用adbWireless无线调试Android真机设备[转]

    开发Android的朋友都知道,真机调试需要把手机与PC相连,然后把应用部署到真机上进行安装和调试.长长的USB线显得很麻烦,而且如果需要USB接口与其他设备连接的话显得很不方便.今天介绍一种不通过U ...

  3. DataView RowFilter

    DataView类用来表示定制的DataTable的视图. DataTable和DataView的关系是遵循著名的设计模式--文档/视图模式,其中DataTable是文档,而Dataview是视图. ...

  4. jQuery中常用的元素查找方法总结

    $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div&q ...

  5. Django疑难问题

    1页面出现中文报错 :Non-ASCII character '\xe9' in file E:\CPaas\cpaas\views.py 解决:在页面顶部加入#coding=utf-8 2执行syn ...

  6. postman传递参数的问题

    postman是一款通过post或者get发送请求测试代码的工具 如果是类的话,就选择JSON格式,如果是一个字段的方法,就直接写入方法值就好了比如 public PageResult<Info ...

  7. UVALive 4727 Jump(约瑟夫环,递推)

    分析: 如果问题是要求最后一个删除的数,重新编号为0到n-1,f[n]表示答案,那么f[n] = (f[n-1]+k)%n. 因为删掉下标k-1以后可以从下标k重新编号为0. 在这个问题只需要推出最后 ...

  8. 【洛谷4149】[IOI2011] Race(点分治)

    点此看题面 大致题意: 给你一棵树,问长度为\(K\)的路径至少由几条边构成. 点分治 这题应该比较显然是点分治. 主要思路 与常见的点分治套路一样,由于\(K≤1000000\),因此我们可以考虑开 ...

  9. 在RichTextBox控件中替换文本文字

    实现效果: 知识运用: RichTextBox控件的SelectedText属性 实现代码: private void button1_Click(object sender, EventArgs e ...

  10. 微信公众帐号开发之一(java)

    闲来没事,就记录一下微信公众平台的开发吧~ 其实微信公众平台开发没有想象中的那么困难,因为注册了微信公众平台帐号登录之后在开发者模式里有详细的文档,个人感觉介绍还是比较详细的. 微信公众平台订阅号和服 ...