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. CocoaPods的ruby问题 Error fetching http://ruby.taobao.org/:

    今天安装了一个CocoaPods,在安装淘宝ruby是遇到了问题 bogon:~ zhch$ gem sources -a http://ruby.taobao.org/ Error fetching ...

  2. Linux命令-用户管理:useradd,userdel,usermod,id,su,env,exit

    添加一个linux用户之后,相当于在linux系统里面创建了如下文件: 添加一个用户(默认也会创建一个同名的用户组,在linux下面用户默认必须在一个用户组里面): useradd wyp 添加用户w ...

  3. Shell基础知识和编程规范

    一,Shell环境查看 1.1 查看系统Shell支持情况 [root@linux-node1 ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin ...

  4. git配置gitignore

    一.背景                                                                                                 ...

  5. jenkins 发送邮件失败

    jenkins 配置发送邮件,发送测试邮件,邮件发送失败: Failed to send out e-mail javax.mail.MessagingException: Could not con ...

  6. javascript Set data structures

    集合(set)是一组无序的,但彼此之间又有一定相关性的数据集.每个成员在数组中只能出现一次. 在使用集合(set)之前最好先理解一下内容: 1.不包含任何成员的集合称为空集合. 2.如果两个集合的成员 ...

  7. 修改easyui的easyloader的默认css目录路径

    easyloader默认情况下会使用js文件所在目录下的themes文件夹中的css,这里改成项目自定义的css文件夹. 首先找到: var m=src.match(/easyloader\.js(\ ...

  8. 将数据库select出来的数据转化为与相应databean相应的字典

    例如以下图: 从user_logs表格select出来的数据放在一个可变数组NSMutableArray中,如 user_logs,如今要 把数据一条条转化为Userlog databean,User ...

  9. iphone CGContextSetLineWidth 画线的问题

    转自:http://blog.csdn.net/jxncwzb/article/details/6267154 CGContextRef context = UIGraphicsGetCurrentC ...

  10. 说实话当一个程序猿不easy

    我以前说过,程序猿不是一般的人,是具有某种超能里的人.但问题是.程序猿往往意识不到自己的这样的特异功能.在他们的眼里.会觉得自己非常普通.跟常人一样,所以,程序猿能做到的事情,其它人--比方他们的客户 ...