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 ...
随机推荐
- 解决Xcode7 iOS9苹果将原http协议改成了https协议问题
在info.plist 加入key <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbi ...
- php 中的$argv与$argc
例如 php test.php as a joke print_r($argv); echo $argc; print_r($argv); array_shift($argv); echo $argc ...
- java:利用数组实现将古诗词纵向输出
java:利用二维数组实现将古诗词纵向输出. 从网络上随便搜索一首古诗词,这里刻意选择句子长短不一的诗词 1.首先先把诗词放进一个二维数组中. 这里将数组类型定义成char 类型,因为将其定义为S ...
- 利用Jenkins自动部署工具间接构建kettle的调度平台
关于Jenkins的介绍我就不说了,自己百度,因为这个工具调用脚本只是他的功能的冰山一角,其他功能我也不能理解,因为不是那个领域. 下面我就介绍一下为什么我们需要一个调度平台,以及学习完 ...
- 初学者学Java(十五)
再谈数组 在这一篇中我们来讲一下关于数组的排序和查找的方法. 排序 说到数组的排序,就不得不说冒泡这种经典的方法. 1.冒泡排序 冒泡排序的基本思想是比较两个相邻元素的值,如果满足条件就交换元素的值( ...
- UITableView显示不全
先上图(不全图片): 正确图片: 原因例如以下: 1.在tableView的父视图的freme问题. 2.tableView本身的frame问题.大小依据自己的实际情况改过来就OK了 希望能够帮助到你 ...
- Android文档资源大放送 感兴趣的话可以网盘下载(个人收集)
Google.Android.SDK开发范例大全.第3版源码.rar http://pan.baidu.com/s/1c0epYzm 精通Android 3中文版(Pro Android 3).pdf ...
- Android实现真正的ViewPager【平滑过渡】+【循环滚动】!!!顺带还有【末页跳转】。
实现真正的ViewPager[平滑过渡]+[循环滚动]!!!顺带还有[末页跳转]. 首先呢, 我要对网上常见的3种ViewPager的循环滚动方法做个概述.急需看真正实现方法的同志请选择性忽略下面这一 ...
- sass笔记-1|Sass是如何帮你又快又好地搞定CSS的
Sass学习笔记持续整理中,开篇不讲怎么安装,sass是什么,这些搜索引擎会告诉你,我们从sass的作用开始讲起,知道sass用来干什么,有什么作用,我们才能相信用sass的好处,并且时时刻刻想着sa ...
- 应用框架 ViewPager Tab Fragment SlidingMenu
介绍 常见的应用框架 框架一:多个tab+Fragment,点击不同的tab加载不同的Fragment,不能滑动切换只能点击切换: 框架二:多个tab+ViewPager+FragmentPagerA ...