这道题目不是太懂,参考了http://www.cnblogs.com/zuoyuan/p/3745126.html的博客。

题意:

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.

解题思路:这题主要是需要深拷贝。看图就明白怎么写程序了。

首先,在原链表的每个节点后面都插入一个新节点,新节点的内容和前面的节点一样。比如上图,1后面插入1,2后面插入2,依次类推。

其次,原链表中的random指针如何映射呢?比如上图中,1节点的random指针指向3,4节点的random指针指向2。如果有一个tmp指针指向1(蓝色),则一条语句:tmp.next.random = tmp.random.next;就可以解决这个问题。

第三步,将新的链表从上图这样的链表中拆分出来。

代码(python):

# Definition for singly-linked list with a random pointer.
# class RandomListNode(object):
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None class Solution(object):
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
if head == None : return head
tmp = head
while tmp:
newNode = RandomListNode(tmp.label)
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,pnew = head,newhead
while pnew.next:
pold.next = pnew.next
pold = pold.next
pnew.next = pold.next
pnew = pnew.next
pold.next = None
return newhead

[LeetCode]题解(python):138-Copy List with Random Pointer的更多相关文章

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

  2. 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)

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

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

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

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

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

  5. Java for LeetCode 138 Copy List with Random Pointer

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

  6. leetcode 138. Copy List with Random Pointer ----- java

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

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

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

  8. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

  9. 138. Copy List with Random Pointer (not do it by myself)

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

  10. 138. Copy List with Random Pointer

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

随机推荐

  1. 【转】CodeGear RAD 2007 SP4

    转自:http://blog.csdn.net/Rzhghost/article/details/2512150 CodeGear RAD 2007 up4最新下载及破解 官方http下载:http: ...

  2. 使用VC++通过远程进程注入来实现HOOK指定进程的某个API

    前阵子读到一篇关于<HOOK API入门之Hook自己程序的MessageBoxW>的博客,博客地址:http://blog.csdn.net/friendan/article/detai ...

  3. 关于C语言中有string类型吗?

    一.问题来源 今天在VS2010平台上,尝试采用scanf() string word; scanf("%s",&word); 然后发现错误,输出采用 printf(&qu ...

  4. iOS移动支付——支付宝支付

    这篇博客总结得很好,我只对在iOS上集成支付宝做简洁的步骤总结. http://www.it165.net/pro/html/201402/9376.html iOS集成支付宝支付的步骤: 准备工作的 ...

  5. ora-14550问题解决

    select a.sid, a.serial#, a.paddr, 'alter system kill session ''' || a.sid || ',' || a.serial# || ''' ...

  6. threadid=1: thread exiting with uncaught exception (group=0x40db8930)

    异常信息如下: 07-26 17:23:49.521: W/dalvikvm(29229): threadid=1: thread exiting with uncaught exception (g ...

  7. echarts实现上海地域PM值(map、timeline)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. vb socket的使用

    说明:原本在 csdn 博客 写博客的,因为使用的移动宽带,csdn的 博客无法访问,所以先暂时到博客园写博客了 有能解决移动宽带 有部分网站不能访问的问题,请联系我,QQ 809775607 /** ...

  9. 关于ODI agent的配置部署

    分类: Linux 最近,做了几个ODI项目的部署,发现ODI agent所在的位置对整个E-LT工作的影响还是比较大的,根据Oracle的官方说法,agent一般需要部署在目标端的数据库服务器上,或 ...

  10. Oracle EBS-SQL (SYS-1): sysadmin_用户职责查询.sql

    select fu.user_name 用户名, fu.description 用户说明, frv.RESPONSIBILITY_NAME 职责名称, REQUEST_GROUP_NAME 报表组, ...