leetcode 138. Copy List with Random Pointer复杂链表的复制
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 not head:
return None
#复制节点本身
cur=head
while cur:
clone=RandomListNode(cur.label)
nextNode=cur.next
cur.next=clone
clone.next=nextNode
cur=nextNode
#复制随机指针
cur=head
while cur:
if cur.random:
cur.next.random=cur.random.next
else:
cur.next.random=None
cur=cur.next.next
#拆开
cur=head
pClone=head.next
while cur:
clone=cur.next
cur.next=clone.next
if clone.next:
clone.next=clone.next.next
else:clone.next=None
cur=cur.next
return pClone
因为一开始没看明白题目所以直接看答案了,发现原来就是插入链表节点再把链表拆开,python代码为粘贴自别人,下次用C++实现贴在后面,
leetcode 138. Copy List with Random Pointer复杂链表的复制的更多相关文章
- [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 拷贝带随机指针的链表
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
原题地址 非常巧妙的方法,不需要用map,只需要O(1)的额外存储空间,分为3步: 1. 先复制链表,但是这个复制比较特殊,每个新复制的节点添加在原节点的后面,相当于"加塞"2. ...
- [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表
public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...
- 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 Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- 真香系列之 Golang 升级
Golang 以前的依赖管理一直饱受诟病,社区的方案也层出不穷,比如 vendor, glide, godep 等.之前的依赖管理一直是依靠 GOPATH 或者将依赖代码下载到本地,这种方式都有劣势. ...
- 2-Elasticsearch原理
参考知乎大佬:https://zhuanlan.zhihu.com/p/62892586 一.倒排索引 倒排索引也叫反向索引,举个例子,理解一下.叫你背一首<静夜思>,立马可以背出,但是叫 ...
- curry&unCurry函数
unCurry函数与curry函数区别:curry化函数:固定部分参数,返回一个接受剩余参数的新函数,目的是为了缩小适用范围,创建一个针对性更强的函数. unCurry化函数:扩大适用范围,创建一个应 ...
- NoSQL与其常见的产品
一. 什么是NoSQL NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL",它是一种非关系型数据库. 二. 为什么要有NoSQL 在现代的计算系统上每 ...
- HashMap 的实现原理(1.7)
参考 :http://wiki.jikexueyuan.com/project/java-collection/hashmap.html https://blog.csdn.net/w22981192 ...
- (转) MiniUI使用
来源于https://my.oschina.net/yunsy/blog/542597 1.MiniUI页签定位 <body> <input name = "bizType ...
- flex布局总结回顾
1.背景 传统css盒式模型,依赖 display属性 + position属性 + float属性实现页面的布局,而随着互联网的迅猛发展,带动了无数的互联网创业者和互联网产品,因而样式布局的美化成为 ...
- Codeforces Round #593 (Div. 2) C. Labs A. Stones
题目:https://codeforces.com/contest/1236/problem/A 思路:两种操作收益都是3 且都会消耗b 操作2对b消耗较小 则可优先选择操作2 再进行操作1 即可得到 ...
- xmlns, xmlns:xsi, xsi:schemaLocation 解释
xmlns, xmlns:xsi, xsi:schemaLocation 解释 xmlnsxsischemaLocation 我们在写 xml 文件时,尤其是 spring .mybatis 的配置文 ...
- Python面向对象的三大特性之继承和组合
继承和组合 一.组合 组合:组合指的是,在一个类中以另外一个类的对象(也就是实例)作为数据属性,称为类的组合 也就是说:一个类的属性是另一个类的对象,就是组合 例子: 圆环是由两个圆组成的,圆环的面积 ...