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 ...
随机推荐
- php-fpm内存泄漏问题排查
生产环境内存泄漏问题排查,以下是排查思路 生产环境上有严重的内存溢出问题(红色框所示,正常值应为是 20M 左右)同时系统有 Core Dump 文件产生排查过程中还发现一个现象,如果关闭 OPc ...
- vue引入jquery插件
在vue中使用jquery插件 1.引入jquery 第一种方法:全局引入jquery 在webpack.base.conf.js,新增以下代码 plugins: [ new webpack.opti ...
- 国产芯片选型手册及厂商名录 版本V2019
- linux下mysql5.7的MHA高可用架构搭建
一.MHA简介 MHA(Master High Availability)目前在mysql高可用方面比较成熟.是一套优秀的作为 mysql高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障 ...
- netlink对中断的支持
http://blog.chinaunix.net/uid-24227137-id-3025783.html https://blog.csdn.net/tycoon1988/article/deta ...
- Android开发艺术探索笔记之Activity
内容来源:Android开发艺术探索第一章:Activity的生命周期与启动模式 不能在onPause中做重量级的操作,因为必须执行完成以后新Activity才能Resume.onPause和onSt ...
- 牛客OI周赛13-提高组 比赛总结
比赛情况 1h才写出T1 100pts + T2 50pts(都是简单dp可还行).然后就去颓废了.颓废完来康康T3的暴力,wow,T3咋这么难呢!?期望概率好像不太会了,退了吧qwq. 所以最后 1 ...
- element-ui + redis + mongo + nuxt
用户注册: let {username,password} = req.body; let u = await UserModel.findOne({username}); if(u){ res.js ...
- Python模块-requests模块使用
写在前面 这篇文章是我照着廖雪峰python网站学习的,大致内容差不多,多了我一丢丢的自己的想法.如果发现有什么不对的话请及时联系我.qq:472668561 参考链接:https://www.lia ...
- Python之常用模二(时间、序列号等等)
一.time模块 表示时间的三种方式: 时间戳:数字(计算机能认识的) 时间字符串:t='2012-12-12' 结构化时间:time.struct_time(tm_year=2017, tm_mon ...