[LeetCode]题解(python):138-Copy List with Random Pointer
这道题目不是太懂,参考了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的更多相关文章
- 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 ...
- 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
- 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 ...
- 138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- node.js(六) UTIL模块
1.inspect函数的基本用法 util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换为字符串的函数,通常用于调试和错误输出.它至少 ...
- 用过滤器和装饰者设计模式(静态代理)解决getParameter乱码问题
post的乱码问题比较好解决,这里主要是对get请求的乱码做处理 解决思路:增强request对象的getParameter方法,使之 getParameter 直接获取到的就是解决乱码后的数据 有 ...
- 关于debug和release 以及new 和delete
题目:给出一组字符串 输入:"ate","eat","Eat","new","ENW",“wha” ...
- NOIP第二次模拟赛 stage1【划分数列(seq.pas/c/cpp)
7划分数列(seq.pas/c/cpp) [题目描述] 给你一个有n个元素的数列,要求把它划分成k段,使每段元素和的最大值最小 [输入格式] 第一行两个正整数n,k 第二行为此数列ai [输出格式] ...
- Java中的String,StringBuffer,StringBuilder详解与区别
1.String Java中string类是不可变的,其中在声明的源代码中用的final,所以只能声明一次.所以每次在明面上的改变其实是重新生成一个String对象,指针指向新的String对象.同时 ...
- Art of Unit Test (1) - Breaking Dependency
#!/usr/bin/env python # encoding: utf-8 import unittest """ the simplyest way to test ...
- python-摩斯码转换
意义:简单实现摩斯码的破译和生成 代码: #-*- coding: UTF-8 -*- ' __date__ = '2016/2/2' import pprint import re chars = ...
- windows常用环境变量
%ALLUSERSPROFILE%列出所有用户Profile文件位置. %APPDATA%列出应用程序数据的默认存放位置. %CD%列出当前目录. %CLIENTNAME%列出联接到终端服务会话时客户 ...
- J2SE知识点摘记-数据库(一)
一. 数据库连接 在JDBC的操作过程中,进行数据库连接的主要步骤如下: 通过Class.forName()加载数据库的驱动程序.首先需要利用来自Class类中的静态方法forNam ...
- Oracle EBS-SQL (BOM-7):检查有BOM无工艺路线的子装配件或成品.sql
select msi.segment1, msi.description, msi.item_typefrom inv.mtl_system_items_b msiwher ...