leetcode 【 Copy List with Random Pointer 】 python 实现
题目:
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
- # 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 is None:
- return head
- # insert newnode between every two nodes between oldlist
- p = head
- while p is not None:
- newnode = RandomListNode(p.label)
- tmp = p.next
- p.next = newnode
- newnode.next = tmp
- p = tmp
- # copy random point
- p = head
- while p is not None:
- if p.random is not None:
- p.next.random = p.random.next
- p = p.next.next
- # extract the new list from mixed list
- newhead = head.next
- p = head
- while p is not None:
- tmp = p.next
- p.next = p.next.next
- p = p.next
- if tmp.next:
- tmp.next = tmp.next.next
- tmp = tmp.next
- return newhead
思路:
自己想不出来巧的方法 网上找个靠谱的帖子:
参照上述帖子的思路写的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 实现的更多相关文章
- [leetcode]Copy List with Random Pointer @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- LeetCode——Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode – Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
随机推荐
- <Android 应用 之路> 天气预报(五)
前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...
- Android Broadcast Receive
Broadcast Receive 广播接收(Broadcast Receive)为android的四大组件之一.主要用于监听广播消息,并做出响应.与应用程序中监听事件相比而言,该监听事件为全局监听. ...
- 五、c++实现离散傅里叶变换
C++离散傅里叶变换 一.序言: 该教程基于之前的图像处理类MYCV,是对其的补充. 二.设计目标 对图像进行简单的离散傅里叶变换,并输出生成的频谱图. 三.需要提前掌握的知识 二维傅里叶变换公式: ...
- cms-帖子幻灯图片上传
package com.open1111.controller.admin; import java.io.File;import java.util.Date;import java.util.Ha ...
- vuejs数据和事件
<body> <div id='root'>{{number}}</div> <script> new Vue({ el:'#root', data:{ ...
- IE中iframe跨域访问
http://blog.csdn.net/ghsau/article/details/13747943
- Flutter 入坑(1):flutter 环境搭建,window版本
下载安装JAVA环境 1. 既然要做原生应用了,而且是基于Android的,那还是需要我们安装一下JAVA的环境的,我比一般得到一个新系统后首先做的就是这一步. https://www.orac ...
- OO终章
一,第四单元架构设计 第一次作业:只有类图 1,重置MyClass,MyOperation等类,为使里面只有必要数据(name,id,visibility等).或方便组织数据(如MyClass作为其底 ...
- android studio 安装以及遇到的一些问题
1 安装 jkd ,版本一般是最新的,下怎么样的看一下自己电脑符合那种要求,可以去官网下 https://www.oracle.com/technetwork/java/javase/download ...
- kernel
http://sebastianraschka.com/Articles/2014_kernel_pca.html