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

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

代码:

  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 == None: return None
  13. tmp = head
  14. while tmp:
  15. newNode = RandomListNode(tmp.label)
  16. newNode.next = tmp.next
  17. tmp.next = newNode
  18. tmp = tmp.next.next
  19. tmp = head
  20. while tmp:
  21. if tmp.random:
  22. tmp.next.random = tmp.random.next
  23. tmp = tmp.next.next
  24. newhead = head.next
  25. pold = head
  26. pnew = newhead
  27. while pnew.next:
  28. pold.next = pnew.next
  29. pold = pold.next
  30. pnew.next = pold.next
  31. pnew = pnew.next
  32. pold.next = None
  33. pnew.next = None
  34. 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. vdp配置

    转:http://jiangjianlong.blog.51cto.com/3735273/1902879 本文将介绍VDP 6.1.2的部署与配置,主要内容包括部署VDP的OVA模板.初始化配置VD ...

  2. Python如何将RGB图像转换为Pytho灰度图像?

    我正尝试使用matplotlib读取RGB图像并将其转换为灰度.在matlab中,我使用这个: 1 img = rgb2gray(imread('image.png')); 在matplotlib t ...

  3. 数据准备<5>:变量筛选-实战篇

    在上一篇文章<数据准备<4>:变量筛选-理论篇>中,我们介绍了变量筛选的三种方法:基于经验的方法.基于统计的方法和基于机器学习的方法,本文将介绍后两种方法在Python(skl ...

  4. [BZOJ4565][HAOI2016]字符合并(区间状压DP)

    https://blog.csdn.net/xyz32768/article/details/81591955 首先区间DP和状压DP是比较明显的,设f[L][R][S]为将[L,R]这一段独立操作最 ...

  5. hdu 2476 区间dp

    题意: 给出两个串s1和s2,一次只能将一个区间刷一次,问最少几次能让s1=s2 例如zzzzzfzzzzz,长度为11,我们就将下标看做0~10 先将0~10刷一次,变成aaaaaaaaaaa 1~ ...

  6. opencv 支持向量机SVM分类器

    支持向量机SVM是从线性可分情况下的最优分类面提出的.所谓最优分类,就是要求分类线不但能够将两类无错误的分开,而且两类之间的分类间隔最大,前者是保证经验风险最小(为0),而通过后面的讨论我们看到,使分 ...

  7. Lucene_索引(域)的查询

    package cn.tz.lucene; import java.io.File; import org.apache.lucene.analysis.Analyzer; import org.ap ...

  8. PostgreSQL教程收集(中文文档/命令行工具/常用命令)

    http://www.postgres.cn/docs/9.6/index.html(中文文档) https://www.postgresql.org/docs/10/static/auth-meth ...

  9. MVC自定义路由01-为什么需要自定义路由

    本篇体验自定义路由以及了解为什么需要自定义路由. 准备 □ View Models using System.Collections.Generic;   namespace MvcApplicati ...

  10. 事物的隔离级别与并发完美体现了cap理论(确保数据完整、安全、一致性,在此基础上实现高性能访问(鱼和熊掌不可兼得)

    事物的隔离级别与并发完美体现了cap理论(确保数据完整.安全.一致性,在此基础上实现高性能访问(鱼和熊掌不可兼得)