LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer
Web Link
http://www.lintcode.com/en/problem/copy-list-with-random-pointer/
Description
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.
- Challenge
Could you solve it with O(1) space?
Code - C++
/**
* 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:
/**
* @param head: The head of linked list with a random pointer.
* @return: A new head of a deep copy of the list.
*/
void copyNext(RandomListNode* head) {
while (head != NULL) {
RandomListNode* newNode = new RandomListNode(head->label);
newNode->random = head->random;
newNode->next = head->next;
head->next = newNode;
head = head->next->next;
}
}
void copyRandom(RandomListNode* head) {
while (head != NULL) {
if (head->next->random != NULL) {
head->next->random = head->random->next;
}
head = head->next->next;
}
}
RandomListNode* splitList(RandomListNode* head) {
RandomListNode* newHead = head->next;
while (head != NULL) {
RandomListNode* temp = head->next;
head->next = temp->next;
head = head->next;
if (temp->next != NULL) {
temp->next = head->next;
}
}
return newHead;
}
RandomListNode *copyRandomList(RandomListNode *head) {
// write your code here
if (head == NULL) {
return NULL;
}
copyNext(head);
copyRandom(head);
return splitList(head);
}
};
Tips:
- None
LintCode - Copy List with Random Pointer的更多相关文章
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【Lintcode】105.Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- Lintcode105 Copy List with Random Pointer solution 题解
[题目描述] A linked list is given such that each node contains an additional random pointer which could ...
- [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(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- Foundations of Machine Learning: The Margin Explanation for Boosting's Effectiveness
Foundations of Machine Learning: The Margin Explanation for Boosting's Effectiveness 在这一节,我们要回答的一个问题 ...
- php Zend虚拟机
在前⾯的章节中,我们了解到⼀个PHP⽂件在服务器端的执⾏过程包括以下两个⼤的过程:1. 递给php程序需要执⾏的⽂件, php程序完成基本的准备⼯作后启动PHP及Zend引擎, 加载注册的扩展模块.2 ...
- linux下socket编程常用头文件
sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构netinet/in.h:定义数据结构sockaddr_inarpa/inet.h:提供IP地址转换函数n ...
- android 手机红外遥控器实现
经过连续几天的编制,安卓手机代码终于完成了,目前已经将我宿舍,家里,集控室的红外遥控电气设备完好的遥控了,另外还遥控了我的D7000相机,不错终于完工了.代码分为二类:各种电视.相机.等等遥控编码最简 ...
- Jsp应用EL和JSTL实例对比。
普通方式: register.jsp <%@ page language="java" import="java.util.*" pageEncoding ...
- [Android&Java]浅谈设计模式-代码篇:观察者模式Observer
观察者,就如同一个人,对非常多东西都感兴趣,就好像音乐.电子产品.Game.股票等,这些东西的变化都能引起爱好者们的注意并时刻关注他们.在代码中.我们也有这种一种方式来设计一些好玩的思想来.今天就写个 ...
- 2-05. 求集合数据的均方差(15) (数学啊 ZJU_PAT)
题目链接:http://pat.zju.edu.cn/contests/ds/2-05 设计函数求N个给定整数的均方差.若将N个数A[]的平均值记为Avg,则均方差计算公式为: 输入格式说明: 第1行 ...
- Python2 元组 cmp() 方法
描述 Python2 元组 cmp() 方法用于比较两个元组,如果 T1< T2返回 -1, 如果 T1== T2返回 0, 如果 T1> T2返回 1. 语法 cmp() 方法语法: c ...
- php_memcache 缓存 下载-安装-配置-学习
一.安装php_memcache.dll 打开phpinfo() 查看PHP Extension Build,如TS,VC11 查看Architecture,如X86.X64 查看PHP版本,如5.6 ...
- django中templates阅读笔记
一.基本知识 1.模版是独立于django的,可以独立运行. 模版变量是用两个大括号括起来的字符串,表示变量.例如{{ person_name }} 模版标签,是用一对大括号和一对百分号括起来的,例如 ...