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复杂链表的复制的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. Leetcode#138 Copy List with Random Pointer

    原题地址 非常巧妙的方法,不需要用map,只需要O(1)的额外存储空间,分为3步: 1. 先复制链表,但是这个复制比较特殊,每个新复制的节点添加在原节点的后面,相当于"加塞"2. ...

  6. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

  7. 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 ...

  8. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  9. 【LeetCode】138. Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

随机推荐

  1. 2-Elasticsearch原理

    参考知乎大佬:https://zhuanlan.zhihu.com/p/62892586 一.倒排索引 倒排索引也叫反向索引,举个例子,理解一下.叫你背一首<静夜思>,立马可以背出,但是叫 ...

  2. Centos7安装RocketMQ4.4

    网上的教程坑贼鸡儿多 一.安装maven RocketMQ依赖maven打包,所以先要在虚拟机中安装maven,我使用的是v3.3.9. 1:进入指定目录下载maven 包 cd /usr/local ...

  3. Linux学习--第十三天--日志、系统运行级别、grub加密

    日志 rsyslogd取代了syslogd. /var/log/cron #定时任务相关日志 /var/log/cups #打印信息相关日志 /var/log/dmesg #开机内核自检相关日志,dm ...

  4. QWidget 设置背景图片

    QWidget 设置背景图片办法: 利用 QPaltette QPixmap pixmap("back.png"); QPalette palette; palette.setBr ...

  5. Gcd HDU - 6545 (基础数论)

    wls 有一个整数 n,他想将 1 − n 这 n 个数字分成两组,每一组至少有一个数,并且使得两组数字的和的最大公约数最大,请输出最大的最大公约数. Input 输入一行一个整数 n. 2 ≤ n ...

  6. oracle 创建表的规则

    1.表明首字母 应该为字母 2.表名的最大长度为30个字符 3.不能使用oracle保留字和关键字来作表名 4.同一用户下的不同表不能具有相同的名称 5.可以使用下划线.数字和字母,但不能使用空格与单 ...

  7. 八、asynicio模块以及爬虫应用asynicio模块(高性能爬虫)

    asynicio模块以及爬虫应用asynicio模块(高性能爬虫) 一.背景知识 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,只用一个线程且采用串行的方式执行, ...

  8. k-means伪代码

    1.初始化k个簇中心. 2.更新所有样本点簇归属:样本点到哪个簇中心点最近就属于哪个簇. 3.重新计算每个簇的中心点(直到簇中心点不再变化或达到更新最大次数) #k-means伪代码 import n ...

  9. 容器"共享"宿主机的hosts文件(终极方案)

    0.背景 有时候制作docker镜像生成容器时需要宿主机的hosts文件共享到容器中.首先想的是通过挂载的方式共享hosts文件,但是实践时发现根本行不通,hosts文件在/etc/目录下,如进行挂载 ...

  10. spark性能调优点(逐步完善)

    1.使用高性能序列化类库2.优化数据结构3.对多次使用的RDD进行持久化/CheckPoint4.使用序列化的持久化级别5.Java虚拟机垃圾回收调优 降低RDD缓存占用空间的比例:new Spark ...