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的更多相关文章

  1. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  2. 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 ...

  3. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  4. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  5. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  6. 【Lintcode】105.Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  7. Lintcode105 Copy List with Random Pointer solution 题解

    [题目描述] A linked list is given such that each node contains an additional random pointer which could ...

  8. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

随机推荐

  1. hadoop编译map/reduce时的问题

    参考链接 http://hadoop.apache.org/common/docs/stable/mapred_tutorial.html http://blog.endlesscode.com/20 ...

  2. 数据存储的两种方式:Cookie 和Web Storage

    数据存储的两种方式:Cookie 和Web Storage 1.Cookie Cookie的作用就像你去超市购物时,第一次给你办张购物卡,这个购物卡里存放了一些你的个人信息,下次你再来这个连锁超市时, ...

  3. java JAXB + STAX(是一种针对XML的流式拉分析API)读取xml

    JDK1.5需要添加jar包,1.6以后就不需要了<dependency> <groupId>stax</groupId> <artifactId>st ...

  4. SYS_R12 MOAC多组织底层技术实现技术分析(Oracle VPD) (案例)

    2014-05-30 Created By BaoXinjian

  5. linux内存回收机制

    无论计算机上有多少内存都是不够的,因而linux kernel需要回收一些很少使用的内存页面来保证系统持续有内存使用.页面回收的方式有页回写.页交换和页丢弃三种方式:如果一个很少使用的页的后备存储器是 ...

  6. Linux内核(10) - 内核中的链表

    早上上班坐地铁要排队,到了公司楼下等电梯要排队,中午吃饭要排队,下班了追求一个女孩子也要排队,甚至在网上下载个什么门的短片也要排队,每次看见人群排成一条长龙时,才真正意识到自己是龙的传人.那么下面咱们 ...

  7. Java 异常模型综述

    一. 异常的引入及基础 发现错误的理想时机是在编译阶段.也就是在你试图运行程序之前. 然而,编译期间编译器并不能找出全部的错误,余下的错误仅仅有在运行期才干发现和解决,这类错误就是 Throwable ...

  8. js 数据结构-栈与队列

    /*[客栈的盘子/月井里的货物,后进先出]栈顶:最先入口/出口的位置栈底:最慢最晚出栈的位置*/ function Stack() { var item = []; //推(将货物推入月井) this ...

  9. 安装ubuntu和windows双系统后,如何修改默认启动项

    在安装了Ubuntu16.04系统之后,系统会默认自启动Ubuntu16.04,而我们大多数情况下可能都在使用windows系统,不修改默认设置,不经意间便会启动了Ubuntu16.04,通过我的经历 ...

  10. mysql-5.7 innodb_file_per_table 详解

    一.innodb_file_per_table 的简要说明: 在很久很久以前也就是说还没有innodb_file_per_table 的那个年代,所有的innodb表的数据都是保存在innodb系统表 ...