leetcode 之Copy List with Random Pointer(23)

深拷贝一个链表,不同的是这个链表有个额外的随机指针。参考:http://blog.csdn.net/ljiabin/article/details/39054999
做法非常的巧妙,分成三步,一是新建结点,并放在旧结点之后;二是修改新结点的random指针;三是将新旧链表断开。
RandomListNode *randomList(RandomListNode *head)
{
//复制每个结点,并将新结点放在旧结点之后
for (RandomListNode *cur = head; cur != nullptr;)
{
RandomListNode *node = new RandomListNode(cur->label);
node->next = cur->next;
cur->next = node;
cur = node->next;
} //修改新结点的Random指针
for (RandomListNode *cur = head; cur != nullptr;)
{
if (cur->random != nullptr)cur->next->random = cur->random->next; cur = cur->next->next;
} //将新旧链表断开
RandomListNode dummy(-);
for (RandomListNode *cur = head,*new_cur=&dummy; cur != nullptr;)
{
new_cur->next = cur->next;
new_cur = new_cur->next; cur->next = cur->next->next;
cur = cur->next; } return dummy.next;
}
leetcode 之Copy List with Random Pointer(23)的更多相关文章
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 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 拷贝带随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【leetcode】Copy List with Random Pointer (hard)
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 _ Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 【 Copy List with Random Pointer 】 python 实现
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
随机推荐
- 【BZOJ4894】天赋(矩阵树定理)
[BZOJ4894]天赋(矩阵树定理) 题面 BZOJ Description 小明有许多潜在的天赋,他希望学习这些天赋来变得更强.正如许多游戏中一样,小明也有n种潜在的天赋,但有 一些天赋必须是要有 ...
- Android Json解析与总结
一.JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Progra ...
- Codeforces Round #341 (Div. 2)A
A. Wet Shark and Odd and Even time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Connections between cities LCA
Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...
- stout代码分析之三:Option类
为了安全表示NULL, stout实现了Option类.Option对象有两种状态: enum State { SOME, NONE, }; 其中SOME表示非空,NONE表示为空.可通过isSome ...
- Hadoop Yarn事件处理框架源码分析
由于想在项目中使用类似yarn的事件处理机制,就看了实现.主要是由Dispatcher.java,EventHandler.java,Service.java这3个类撑起来的. 在事件处理之前,先注册 ...
- Eclipse中安装插件的三种方式
转载自:http://www.cnblogs.com/lcgustc/archive/2013/01/03/2843496.html Eclipse也用了很久,但是都没有怎么去研究过怎么安装插件,今天 ...
- redis常用数据结构解析
Redis是一个开源的Key-Value存储引擎,它支持string.hash.list.set和sorted set等多种值类型.由于其卓越的性能表现.丰富的数据类型及稳定性,广泛用于各种需要k/v ...
- Python编写在Maya中查看文件列表的插件
之前写过一篇用Python遍历文件夹的文章,今天把代码扩展一下,做成一个有UI用户界面的Maya插件,可以直接在Maya中运行: 功能是显示磁盘分区目录下的文件列表,通过定制也可以查看任意目录下的文件 ...
- JVM-内存回收算法--复制算法
复制算法,它将堆上的内存分为两个大小相等的区域,一个是空闲区域,一个是活动区域.在程序运行中,实际使用的是活动区域,也就是有50%的空间被浪费掉. 复制算法的实现过程:1.找出活动空间中所有存活的对象 ...