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

 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 is None:
13             return head
14
15         # insert newnode between every two nodes between oldlist
16         p = head
17         while p is not None:
18             newnode = RandomListNode(p.label)
19             tmp = p.next
20             p.next = newnode
21             newnode.next = tmp
22             p = tmp
23
24         # copy random point
25         p = head
26         while p is not None:
27             if p.random is not None:
28                 p.next.random = p.random.next
29             p = p.next.next
30
31         # extract the new list from mixed list
32         newhead = head.next
33         p = head
34         while p is not None:
35             tmp = p.next
36             p.next = p.next.next
37             p = p.next
38             if tmp.next:
39                 tmp.next = tmp.next.next
40             tmp = tmp.next
41
42         return newhead

思路

自己想不出来巧的方法 网上找个靠谱的帖子:

http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5ODIzNDQ3Mw==&appmsgid=10000291&itemidx=1&sign=ccde63918a24dee181f1fd1a4e3e6781

参照上述帖子的思路写的python代码。

遇到的一个问题是,一开始判断极端case的时候有“if head.next is None: return head”

结果一直报错,后来去掉后AC了。注意一个点的时候也要复制。

还有就是,一直对python里面变量间的赋值不太清楚,google了一篇如下的日志,讲的比较靠谱一些。

http://www.cnblogs.com/evening/archive/2012/04/11/2442788.html

时间: 2024-12-21 17:03:15

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 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. 解题思路:这题主要是需要深

LeetCode: Copy List with Random Pointer [138]

[题目] 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. [题意] 给定一个链表,每个节点除了next指针外,还有一个random指针,指向任意的节点. 要求,复制这样的一个链表 [思路] 思路1: 先一次生成每个节点对

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目:给定一个链表,当中的每一个节

[LeetCode]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. 利用hashMap存储,原始的node作为key,new的node作为value,这样就建立了新旧链表的以一一对应关系: /** * Definition for sing

[LeetCode] 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. 这道链表的深度拷贝题的难点就在于如何处理随机指针的问题,由于每一个节点都有一个随机指针,这个指针可以为空,也可以指向链表的任意一个节点,如果我们在每生成一个新节点给其随机指

LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

问题: 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.   结点的定义如下: /** * Definition for singly-linked list with a random pointer. * class

[LeetCode]Copy List with Random Pointer复杂链表的复制

/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} * }; */ class Solution {//为了能够快速定位某个节点,采用确定性映射的方式,将

leetcode - Copy List with Random Pointer题解

链表格式 通常链表就是一个值,一个next指针,指向后面的节点. 结构体如下: struct Node{ int val; struct Node* next; } 这个题目里的节点多了一个指针,除了指向下一个节点的next指针,还有一个指向这个链表随机一个节点的random指针. 结构体如下: struct Node{ int val; struct Node* next; struct Node* random; } 普通链表值拷贝 通常我们拷贝链表的时候,进行值拷贝,只用从头开始遍历就可以

【LeetCode】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. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

Copy List with Random Pointer leetcode java

题目: 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. 题解: 如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描li