原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/

题意:

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;就可以解决这个问题。

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

代码:

# 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 == None: return None
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 = 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]Copy List with Random Pointer @ Python的更多相关文章

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

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

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

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

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

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

  5. Leetcode 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] 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 &amp;Clone Graph 复杂链表的复制&amp;图的复制

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

  9. [Leetcode Week17]Copy List with Random Pointer

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

随机推荐

  1. jQuery的data() 和 h5 的 dataset()

    作用:获取自定义属性 命名规则:驼峰命名法  data-user==>user data-user-name==>userName 区别:jQuery:操作内存   h5: 操作DOM j ...

  2. 跟厂长学PHP7内核(二):源码分析的环境与工具

    本文主要介绍分析源码的方式,其中包含环境的搭建.分析工具的安装以及源码调试的基本操作. 一.工具清单 PHP7.0.12 GDB CLion 二.源码下载及安装 $ wget http://php.n ...

  3. OSI、TCP、IP、UDP 这些都是啥??

    一个大大的问号首先抛出,计算机之间是如何进行通信的? 计算机网络是通过传输介质.通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来,实现资源共享和数据传输的系统. 网络协议就是数据按照一定的 ...

  4. 关于Python Profilers性能分析器

    1. 介绍性能分析器 作者:btchenguang profiler是一个程序,用来描述运行时的程序性能,并且从不同方面提供统计数据加以表述.Python中含有3个模块提供这样的功能,分别是cProf ...

  5. xml javascript

    1.XMLHttpRequest对象 创建XMLHttpRequest对象 xmlhttp=new XMLHttpRequest(); ##老版本的 Internet Explorer (IE5 和 ...

  6. CodeForces1082G Petya and Graph 最小割

    网络流裸题 \(s\)向点连边\((s, i, a[i])\) 给每个边建一个点 边\((u, v, w)\)抽象成\((u, E, inf)\)和\((v, E, inf)\)以及边\((E, t, ...

  7. luogu NOIp热身赛(2018-11-07)题解

    为什么前面的人都跑得那么快啊? QAQ T1:区间方差 题目大意:询问区间方差,支持单点修改 首先把方差的式子展开,得到 $$d = \frac{a_1 + ... a_n}{n} - \frac{a ...

  8. [BZOJ5303][HAOI2018]反色游戏(Tarjan)

    暴力做法是列异或方程组后高斯消元,答案为2^自由元个数,可以得60分.但这个算法已经到此为止了. 从图论的角度考虑这个问题,当原图是一棵树时,可以从叶子开始唯一确定每条边的选择情况,所以答案为1. 于 ...

  9. 让 Git 全局性的忽略 .DS_Store

    让 Git 全局性的忽略 .DS_Store Mac 中每个目录都会有个文件叫.DS_Store, 用于存储当前文件夹的一些 Meta 信息.每次提交代码时,我都要在代码仓库的 .gitignore ...

  10. ClassifyHandler 分类处理结构

      public class ClassifyHandler { public object vTrue { get; set; } public object vFalse { get; set; ...