题目:

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.

题解:

Solution 1 ()

class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (head == nullptr) return nullptr;
RandomListNode* dummy = new RandomListNode(-);
RandomListNode* cur = head;
RandomListNode* node = dummy;
unordered_map<RandomListNode*, RandomListNode*> map;
while (cur) {
RandomListNode* tmp = new RandomListNode(cur->label);
node->next = tmp;
map[cur] = node->next;
node = node->next;
cur = cur->next;
}
node = dummy->next;
cur = head;
while (node) {
node->random = map[cur->random];
node = node->next;
cur = cur->next;
} return dummy->next;
}
};

Solution 2 ()

class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head) return head;
RandomListNode* cur = head;
while (cur) {
RandomListNode* node = new RandomListNode(cur->label);
node->next = cur->next;
cur->next = node;
cur = node->next;
}
cur = head;
while (cur) {
if (cur->random) {
cur->next->random = cur->random->next;
}
cur = cur->next->next;
}
cur = head;
RandomListNode* first = cur->next;
while (cur) {
RandomListNode* tmp = cur->next;
cur->next = tmp->next;
if (tmp->next) {
tmp->next = tmp->next->next;
}
cur = cur->next;
} return first;
}
};

Solution 3 ()

class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *newHead, *l1, *l2;
if (head == NULL) return NULL; for (l1 = head; l1 != NULL; l1 = l1->next) {
l2 = new RandomListNode(l1->label);
l2->next = l1->random;
l1->random = l2;
} newHead = head->random;
for (l1 = head; l1 != NULL; l1 = l1->next) {
l2 = l1->random;
l2->random = l2->next ? l2->next->random : NULL;
} for (l1 = head; l1 != NULL; l1 = l1->next) {
l2 = l1->random;
l1->random = l2->next;
l2->next = l1->next ? l1->next->random : NULL;
} return newHead;
}
};

【Lintcode】105.Copy List with Random Pointer的更多相关文章

  1. 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告

    题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...

  2. 【LeetCode】138. Copy List with Random Pointer

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

  3. 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...

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

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

  5. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  6. 【概率论】3-1:随机变量和分布(Random Variables and Discrete Distributions)

    title: [概率论]3-1:随机变量和分布(Random Variables and Discrete Distributions) categories: Mathematic Probabil ...

  7. 16. Copy List with Random Pointer

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

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

  9. Copy List with Random Pointer leetcode java

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

随机推荐

  1. Laravel 5.4的本地化

    简介 Laravel 的本地化功能提供方便的方法来获取多语言的字符串,让你的网站可以简单的支持多语言. 语言包存放在 resources/lang 目录下的文件里.在此目录中应该有应用对应支持的语言并 ...

  2. spring中的事件 applicationevent 讲的确实不错

    event,listener是observer模式一种体现,在spring 3.0.5中,已经可以使用annotation实现event和eventListner里. 我们以spring-webflo ...

  3. 自己定义ProgressDialog载入图片

    使用系统载入框 mDialog = new ProgressDialog(this); mDialog.setCancelable(true);//能否够被取消 mDialog.setMessage( ...

  4. SQL Server外连接、内连接、交叉连接

    小编在做组织部维护最后收尾工作的时候,遇到了这样一个问题,须要将定性考核得分查出来.定量考核相应的数据查出来并进行得分计算.附加分查出来,最后将这三部分信息汇总之后得到总成绩,假设当中一项成绩没有进行 ...

  5. dubbo介绍及其使用案例

    dubbo介绍及其使用案例 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果 ...

  6. 怎么利用jquery.form 提交form

    说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...

  7. Optimizer in SQL - Catalyst Optimizer in Spark SQL

    SELECT sum(v) FROM (    SELECT score.id, 100+80+score.math_score+ score.english_score AS v    FROM p ...

  8. Android笔记之Retrofit与RxJava的组合

    依赖 implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:con ...

  9. HDUJ 2052 Picture 模拟

    Picture Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  10. [容易]Fizz Buzz 问题

    题目来源:http://www.lintcode.com/zh-cn/problem/fizz-buzz/