【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 ...
随机推荐
- Calendar中add函数和roll函数的用法及区别
Calendar中add()和roll()函数的用法一.取某个时间点后的整点时刻.例如1984年7月7日15:23:05后的整点时刻即为1984-07-07 16:00:00.实现如下:Calenda ...
- Linux 搭建SVN 服务器
一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository) 中 ...
- UESTC_摩天轮 2015 UESTC Training for Dynamic Programming<Problem K>
K - 摩天轮 Time Limit: 10000/4000MS (Java/Others) Memory Limit: 262143/262143KB (Java/Others) Submi ...
- hdu 5491 The Next(暴力枚举)
Problem Description Let L denote the number of 1s in integer D’s binary representation. Given two in ...
- Bridging signals(二分 二分+stl dp)
欢迎参加——每周六晚的BestCoder(有米!) Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6 ...
- 一起talk GDB吧(第七回:GDB监视功能)
各位看官们.大家好.上一回中我们说的是GDB改动程序执行环境的功能.而且说了怎样使用GDB改动变量 的值.这一回中.我们继续介绍GDB的调试功能:监视功能.当然了,我们也会介绍怎样使用GDB的监视功 ...
- Carmack在QUAKE3中使用的计算平方根的函数
// // Carmack在QUAKE3中使用的计算平方根的函数 // float CarmSqrt(float x){ union{ int intPart; float floatPart; } ...
- MySQL Update 使用
备忘: USE `xxx`; ; UPDATE `TB_MB_1` T SET T.`MedicalCount` = ( SELECT S.Total-- ,S.`HospitalID` FROM( ...
- SQL Server服务启动失败,错误代码:10048
今天打开电脑后遇到了一个奇葩的问题,启动Sql Server服务时,出现如下图所示错误:
- 地图:CLGeocoder地址解析与反地址解析
1.导入系统框架