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 ...
随机推荐
- [ZJOI2010]贪吃的老鼠 网络流
---题面--- 题解: 这是一道强题emmmm,做法非常巧妙,,,我也是看了好久大佬题解才看明白一点 首先考虑没有限制的情况,即n个老鼠可以在同一时刻吃同一块奶酪 对各个时间段拆点,连奶酪 ---& ...
- Android 字母导航条实现
在Activity中进行功能的实现,需要用到第三方jar包:pinyin4j.jar,此jar包用于将汉字转换为汉语拼音. 首先,设置右侧边栏索引列表(A-Z),并且设置列表点击,Touch事件,点击 ...
- BZOJ4557:[JLOI2016/SHOI2016]侦察守卫——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4557 小R和B神正在玩一款游戏.这款游戏的地图由N个点和N-1条无向边组成,每条无向边连接两个点, ...
- Qt ------- QByteArray操作注意
使用QByteArray方法把数据存入QByteArray需要是char型数据,如果需要存入无符号8位数据,如下: QByteArray data; data[0] = 0xFF; 即使通过data[ ...
- How to configue session timeout in Hive
This article explains how to configure the following settings in Hive:hive.server2.session.check.int ...
- Nginx的启动、停止、平滑重启
转载自:http://www.xj123.info/2572.html 启动Nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/ngi ...
- Tomcat设置开启时自动访问某个servlet类存在的问题
<servlet> <servlet-name>****</servlet-name> <servlet-class>****</servlet- ...
- eclipse如何远程debug/断开远程debug
eclipse如何远程debug? 当你的代码已经部署到生产或者测试环境的时候,你如何debug判断线上的问题呢? debug之前必须保证本地代码和远程代码完全一致,否则将不能建立连接 在eclips ...
- Windows/Linux javac/java编译运行引入所需的jar包
> Windows 假设要引用的jar放在D:/test目录下,名字为t1.jar, java源文件放在D:/test/src目录下,名字为t2.java. 编译: javac -cp d: ...
- Eclipse Tomcat Project报错:HTTP Status 404错误
想要在eclipse里部署tomcat,结果tomcat单独可以通过连接测试,用eclipse就404了 404肯定都是目录不对,试了半天在eclipse下改了一下配置和文件位置就行了 1.先在菜单栏 ...