leetcode138Copy List with Random Pointer
来源:互联网 发布:mac被设密码了怎么办 编辑:程序博客网 时间:2024/05/21 22:27
class Solution(object):
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
#copyhead=RandomListNode(0)
dic = collections.defaultdict(lambda: RandomListNode(10))
#dic[None] = None
if head==None:
return head
n = head
while n:
dic[n].label = n.label
if n.next==None:
dic[n].next=None
else:
dic[n].next = dic[n.next]
if n.random==None:
dic[n].random=None
else:
dic[n].random = dic[n.random]
n = n.next
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
#copyhead=RandomListNode(0)
dic = collections.defaultdict(lambda: RandomListNode(10))
#dic[None] = None
if head==None:
return head
n = head
while n:
dic[n].label = n.label
if n.next==None:
dic[n].next=None
else:
dic[n].next = dic[n.next]
if n.random==None:
dic[n].random=None
else:
dic[n].random = dic[n.random]
n = n.next
return dic[head]
参考https://discuss.leetcode.com/topic/9557/clear-and-short-python-o-2n-and-o-n-solution/6
0 0
- leetcode138Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- LeetCode_Copy List with Random Pointer
- 【剑指offer】第二题
- java基础--字符串
- 黑盒测试和白盒测试区别
- oracle11g安装教程
- Linux MTD系统剖析
- leetcode138Copy List with Random Pointer
- 设计模式之桥接模式C++实现
- 利用微软Azure云在30分钟内搭建SAP on HANA云环境
- 漫谈递归:递归的效率问题
- 职场逻辑能力提升
- Java8 Lambda表达式教程
- IntelliJ IDEA For Mac 快捷键
- 操作系统之存储管理
- 数组(九)