LeetCode _ 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.
剑指offer里面的一道题,具体思路看不懂请查阅剑指offer
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
void copyFirst(RandomListNode *head){ RandomListNode *pcur, *pnext, *q;
pcur = head;
while(pcur != NULL){
pnext = pcur->next;
q = new RandomListNode(pcur->label);
q->next = pnext;
pcur->next = q;
pcur = pnext;
}
}
void fixRandom(RandomListNode * head){ RandomListNode *ph = head;
while(ph!=NULL){
if(ph->random != NULL)
ph->next->random = ph->random->next;
ph = ph->next->next;
}
}
RandomListNode * getCopy(RandomListNode *head){ RandomListNode *p,*rcur,*q, *res;
res = NULL; p = head;rcur = NULL;
while(p != NULL){
q = p->next;
p->next = q->next;
p = p->next;
if(res == NULL){
res = q;
rcur = q;
}else{
rcur->next = q;
rcur = q;
}
}
// rcur->next = NULL;
return res; }
RandomListNode *copyRandomList(RandomListNode *head) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(head == NULL) return NULL;
copyFirst(head);
fixRandom(head);
return getCopy(head); }
};
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 】 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 ...
随机推荐
- TinyXml高速入门(一)
作者:朱金灿 来源:http://blog.csdn.net/clever101 对于xml文件,眼下我的工作仅仅是集中在配置文件和作为简单的信息文件来用,因此我不太喜欢使用msxml这样的重量级的x ...
- CString与std::string unicode下相互转化
1. CString to string CString str = L"test"; CString stra(str.GetBuffer(0)); str.ReleaseB ...
- 【转】IOS7 MPMoviePlayerViewController横屏显示
在应用程序中用到MPMoviePlayerViewController时,有时需要保持应用程序为竖屏状态,而视频播放器显示为横屏,如何做呢?如果采用强制横屏的方法,应用审核的时候是不会通过的,因为该方 ...
- [转] Linux下查看用户列表
原文地址:http://xiaod.in/read.php?77 俺的centos vps上面不知道添加了多少个账户,今天想清理一下,但是以前还未查看过linux用户列表,google了一下,找到方便 ...
- 线段树---HDU1166敌兵布阵
这个是线段树中最入门的题目,但是由于不了解线段树的概念,当然更不知道怎么样,所以觉得挺费劲,整了一会发现还是基本的思想,就是还是将一个线段继续分割,一直分割到不能分割,这道题目是知道多少个军营,也就是 ...
- MVP模式 详解 案例
介绍 MVC: View:对应于布局文件 Model:业务逻辑和实体模型 Controllor:对应于Activity 实际上关于该布局文件中的数据绑定的操作,事件处理的代码都在Activity中,造 ...
- FileUpload 简单上传+小预览
页面代码 : <form id="form1" runat="server"> <div> <asp:FileUpload ID= ...
- Android--------使用gson解析json文件
##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...
- 有用的前端demo
js定时器循环切换字体和背景颜色<script type="text/javascript"> var flashId = 0; setInterval(functio ...
- 关于JavaScript对象的键和值
一个JavaScript对象由键和值组成. 当一个给定键的值被设置为一个字符串.布尔值.数字.数组或对象时,我们把这个键称为属性. 当把键设置为函数时,我们把它叫做方法.