【LeetCode with Python】 Copy List with Random Pointer
来源:互联网 发布:电脑照相机软件下载 编辑:程序博客网 时间:2024/05/01 16:30
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/copy-list-with-random-pointer/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/39453281
原题页面:https://oj.leetcode.com/problems/copy-list-with-random-pointer/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/39453281
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 RandomListNode: def __init__(self, x): self.label = x self.next = None self.random = Noneclass Solution: # @param head, a RandomListNode # @return a RandomListNode def copyRandomList(self, head): if None == head: return None save_list = [ ] p1 = head while None != p1: save_list.append(p1) p1 = p1.next new_head = RandomListNode(-1) new_head.next = head first = new_head second = head copy_head = RandomListNode(-1) copy_first = copy_head copy_second = None while None != first: copy_second = RandomListNode(second.label) if None != second else None copy_first.next = copy_second copy_first = copy_first.next first = first.next if None != second: second = second.next p1 = head p1_tail = head.next p2 = copy_head.next while None != p1: p1_tail = p1.next p1.next = p2 p2.random = p1 p1 = p1_tail p2 = p2.next p2 = copy_head.next #p1 = head while None != p2: p2.random = p2.random.random.next if None != p2.random.random else None #p1.next = p1.next.next.random if None != p1.next.next else None # may broken the previous p1.next, so have to save ori list p2 = p2.next #p1 = p1.next len_save_list = len(save_list) for i in range(0, len_save_list - 1): save_list[i].next = save_list[i + 1] save_list[len_save_list - 1].next = None return copy_head.next
0 0
- [Leetcode][python]Copy List with Random Pointer
- 【LeetCode with Python】 Copy List with Random Pointer
- [leetcode][list] Copy List with Random Pointer
- leetcode 日经贴,python code -Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode: Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- LeetCode题解:Copy List with Random Pointer
- [LeetCode]Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- leetcode-Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode | Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- StringBuilder 与 StringBuffer 区别
- 【LeetCode with Python】 Combinations
- Directx10 龙书笔记- 雾效的实现
- MariaDB Galera Cluster 5.5.39 on Centos 5.8_64
- 【LeetCode with Python】 Convert Sorted Array to Binary Search Tree
- 【LeetCode with Python】 Copy List with Random Pointer
- 位域之我见
- 【LeetCode with Python】 Count and Say
- 《炉石传说》架构设计赏析(3):Gameplay初探
- 【LeetCode with Python】 Divide Two Integers
- Android SDK目录详解
- 使用Firebug对js进行断点调试的图文方法
- 【LeetCode with Python】 Flatten Binary Tree to Linked List
- postgresql数据导入到mysql