【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
题目意思:
深拷贝一个给定的带random指针的链表,这个random指针可能会指向其他任意一个节点或者是为null。
(又是链表啊啊啊啊啊啊啊!!!!!!!!Orz……)
解题思路:
在每个原来节点的后面插入一个新节点(label值一样),然后复制原来节点的random指针,最后包含新旧链表的长链表分成一个原来的链表和一个新的链表。
有一点需要注意:就是遇到链表的问题时,一定要考虑p是空指针时,不要出现p->next之类的调用,本题也是一样要考虑的。
代码如下:
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(head == NULL)
return NULL;
RandomListNode *p = head;
//第一遍:扫描顺序复制next指针
while(p){
RandomListNode *newNode = new RandomListNode(p->label);
newNode->next = p->next;
p->next = newNode;
p = newNode->next;
}
p = head;
//第二遍:复制random指针
while(p){
if(p->random)
p->next->random = p->random->next;
p = p->next->next;
}
p = head;
//第三遍:恢复旧链表和新链表
RandomListNode *newHead = p->next;
RandomListNode *newP = newHead;
p->next = newP->next;
p = p->next;
//要保证p不是空指针,不然调用p->next就会报错。
while(p){
newP->next = p->next;
newP = newP->next;
p->next = newP->next;
p = p->next;
}
return newHead;
}
};
【LeetCode练习题】Copy List with Random Pointer的更多相关文章
- [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 ...
随机推荐
- PV与并发之间换算的算法 换算公式
- UESTC_Little Deer and Blue Cat CDOJ 1025
In DOTA, there are two Intellegence heroes. One is Enchantress, who is usually called Little Deer by ...
- POJ3268 Silver Cow Party(dijkstra+矩阵转置)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15156 Accepted: 6843 ...
- myeclipse修改tomcat端口
myeclipse自带的tomcat可以直接在myeclipse下面进行配置,但是如果是配置的tomcat 只能自己手动修改了
- AudioManager详解(结合源代码)
AudioManager:用来对音量大小,声音模式(静音,震动,震动加声音等模式)的管理, 还有用它来注册“插入耳机”时的广播接收者(Action: android.intent.action.MED ...
- .Net插件编程模型:MEF和MAF[转载]
.Net插件编程模型:MEF和MAF MEF和MAF都是C#下的插件编程框架,我们通过它们只需简单的配置下源代码就能轻松的实现插件编程概念,设计出可扩展的程序.这真是件美妙的事情! 今天抽了一点时间, ...
- [网络流最大流经典][uva 11082][矩阵解压]
题目大意 分析 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring ...
- 2014年1月24日 Oracle 连接查询与子查询
1.乘积连接: 源表.源数据交叉链接,结果集数量为源数据之间的乘积 2.相等链接: 通过where关联几个数据源中的某一字段进行链接 3.自链接 自己链接自己 SSF A a1, A a2 ...
- Java web 实现 之 Filter分析ip统计网站的访问次数
统计工作需要在所有资源之前都执行,那么就可以放到Filter中了. 我们这个过滤器不打算做拦截操作!因为我们只是用来做统计的. 用什么东西来装载统计的数据.Map<String,Integer& ...
- MYSQL插入处理重复键值的几种方法
当unique列在一个UNIQUE键上插入包含重复值的记录时,默认insert的时候会报1062错误,MYSQL有三种不同的处理方法,下面我们分别介绍. 先建立2个测试表,在id列上创建unique约 ...