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. Linux Pin Control 子系统

    Pin Control Subsystem是Linux内核抽象出的一套用于控制硬件引脚的一套子系统. 1.源文件列表 源码位于linux/drivers/pinctrl目录下,源文件列表如下: 文件名 ...

  2. LevelDB场景分析1--整体结构分析

    基本用法 数据结构 class DBImpl : public DB { private:     struct CompactionState;     struct Writer;// Infor ...

  3. 删除vs中最近的项目的方法

    Microsoft Visual Studio中可以自行设置显示多少个最近打开的项目,但有些时候会建个项目做测试,用完了就删了,却总显示在“文件”-“最近的项目”菜单中以及“起始页”-“打开现有项目” ...

  4. 关于centos7中使用rpm方式安装mysql5.7版本后无法使用root登录的问题

    最近在centos7中通过rpm方式安装了最新版本的mysql-server 5.7 (mysql57-community-release-el7-7.noarch.rpm) ,发现安装成功后无法使用 ...

  5. stm32 外扩SRAM使用问题

    当把外扩SRAM内存拷贝到片上SRAM内存时使用内存拷贝函数memset()或者原子定义的mymemset()函数,编译器会提示空间不足. 原因是这两个函数一个是只能对片上SRAM操作,一个是只能对外 ...

  6. Java中弹出框的集中方式

    1.显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", " ...

  7. Creating the Help Page in ASP.NET Web API

    Introduction In this article we will define the process of creating the help page in the ASP .NET We ...

  8. Linux下的ip命令

    linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  9. SpringDaoSupport

    @Component public class SuperDAO extends HibernateDaoSupport { @Resource(name="sessionFactory&q ...

  10. FPGA三分频,五分频,奇数分频

    我们在做FPGA设计时,有时会用到时钟频率奇数分频的频率,例如笔者FPGA的晶振为50M,当我们需要10M的时钟时,一种方式可以使用DCM或PLL获取,系统会内部分频到10M,但其实VERILOG内部 ...