题目

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

  1. # Definition for singly-linked list with a random pointer.
  2. # class RandomListNode:
  3. # def __init__(self, x):
  4. # self.label = x
  5. # self.next = None
  6. # self.random = None
  7.  
  8. class Solution:
  9. # @param head, a RandomListNode
  10. # @return a RandomListNode
  11. def copyRandomList(self, head):
  12. if head is None:
  13. return head
  14.  
  15. # insert newnode between every two nodes between oldlist
  16. p = head
  17. while p is not None:
  18. newnode = RandomListNode(p.label)
  19. tmp = p.next
  20. p.next = newnode
  21. newnode.next = tmp
  22. p = tmp
  23.  
  24. # copy random point
  25. p = head
  26. while p is not None:
  27. if p.random is not None:
  28. p.next.random = p.random.next
  29. p = p.next.next
  30.  
  31. # extract the new list from mixed list
  32. newhead = head.next
  33. p = head
  34. while p is not None:
  35. tmp = p.next
  36. p.next = p.next.next
  37. p = p.next
  38. if tmp.next:
  39. tmp.next = tmp.next.next
  40. tmp = tmp.next
  41.  
  42. 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. <Android 应用 之路> 天气预报(五)

    前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...

  2. Android Broadcast Receive

    Broadcast Receive 广播接收(Broadcast Receive)为android的四大组件之一.主要用于监听广播消息,并做出响应.与应用程序中监听事件相比而言,该监听事件为全局监听. ...

  3. 五、c++实现离散傅里叶变换

    C++离散傅里叶变换 一.序言: 该教程基于之前的图像处理类MYCV,是对其的补充. 二.设计目标 对图像进行简单的离散傅里叶变换,并输出生成的频谱图. 三.需要提前掌握的知识 二维傅里叶变换公式: ...

  4. cms-帖子幻灯图片上传

    package com.open1111.controller.admin; import java.io.File;import java.util.Date;import java.util.Ha ...

  5. vuejs数据和事件

    <body> <div id='root'>{{number}}</div> <script> new Vue({ el:'#root', data:{ ...

  6. IE中iframe跨域访问

    http://blog.csdn.net/ghsau/article/details/13747943

  7. Flutter 入坑(1):flutter 环境搭建,window版本

    下载安装JAVA环境 1. 既然要做原生应用了,而且是基于Android的,那还是需要我们安装一下JAVA的环境的,我比一般得到一个新系统后首先做的就是这一步.    https://www.orac ...

  8. OO终章

    一,第四单元架构设计 第一次作业:只有类图 1,重置MyClass,MyOperation等类,为使里面只有必要数据(name,id,visibility等).或方便组织数据(如MyClass作为其底 ...

  9. android studio 安装以及遇到的一些问题

    1 安装 jkd ,版本一般是最新的,下怎么样的看一下自己电脑符合那种要求,可以去官网下 https://www.oracle.com/technetwork/java/javase/download ...

  10. kernel

    http://sebastianraschka.com/Articles/2014_kernel_pca.html