1、题目描述

2、问题分析

首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可。

3、代码

 RandomListNode *copyRandomList(RandomListNode *head) {
if( head == NULL){
return NULL;
}
RandomListNode* newhead = new RandomListNode();
RandomListNode* np = newhead; RandomListNode* p = head;
map<RandomListNode*, RandomListNode*> m;
while (p != NULL){
RandomListNode* tmp = new RandomListNode(p->label);
m.insert(make_pair(p,tmp));
np->next = tmp;
np = np->next;
p = p->next;
} p = head ;
np = newhead->next;
while( p != NULL){
np->random = m[p->random];
p = p->next;
np = np->next;
}
return newhead->next; }

LeetCode题解之Copy List with Random Pointer的更多相关文章

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

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

  2. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

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

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

  4. LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)

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

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

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

  6. [Leetcode Week17]Copy List with Random Pointer

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

  7. Copy List with Random Pointer leetcode java

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

  8. 16. Copy List with Random Pointer

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

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

随机推荐

  1. CodeSmith读取数据库

    这两天在看CodeSmith文档,因为官方文档在读数据库这一篇使用的是VB写的,对于C#使用者来说看起来很不方便,所以我改成C#的,顺便写下我自己的使用过程. 首先,要使用CodeSmith连接数据库 ...

  2. 深入聊聊Java多线程

    一.背景 在没有学习Java多线程以前,总觉得多线程是个很神秘的东西,只有那些大神才能驾驭,新年假期没事就来学习和了解一下Java的多线程,本篇博客我们就来从头说一下多线程到底是怎么回事. 二.概述 ...

  3. php -- 数学函数

    ----- 016-math.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv="c ...

  4. 编写自己的SpringBoot-starter

    前言 我们都知道可以使用SpringBoot快速的开发基于Spring框架的项目.由于围绕SpringBoot存在很多开箱即用的Starter依赖,使得我们在开发业务代码时能够非常方便的.不需要过多关 ...

  5. WPF 中textBox实现只输入数字

    刚学到 通过本方法可以使文本框只能输入或复制入数字  对于数量类输入文本框比较有用 金额类只需小改动也可实现 以TextBox txtCount为例 添加TextChanged事件 代码如下 priv ...

  6. SQL 语句语法简介(一)

    语句分类 SQL 命令一般分为三类:DQL.DML.DDL. 一.DDL语句. 1.1建表语句 CREATE TABLE table_name( col01_name data_type, col02 ...

  7. IDEA SQL dialect detection和Duplicated Code检测关闭

    IDEA似乎做的太多,对于Mybatis文件中的SQL语法检查可能就没有太大的必要性,Duplicated Code检测其实非常好,但是我测试使用JDBC代码的时候一堆波浪线让我很不舒服 因此将这两个 ...

  8. Java 8 新特性-菜鸟教程 (1) -Java 8 Lambda 表达式

    Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性. Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中). 使用 Lambda 表达式可以使代码变的更加 ...

  9. JS作用域,作用域,作用链详解

    前言   通过本文,你大概明白作用域,作用域链是什么,毕竟这也算JS中的基本概念. 一.作用域(scope) 什么是作用域,你可以理解为你所声明变量的可用范围,我在某个范围内申明了一个变量,且这个变量 ...

  10. 读jQuery源码释疑笔记2

    本释疑笔记是针对自己在看源码的过程中遇到的一些问题的解答,对大众可能不具有参考性,不过可以看看有没有你也不懂得地方,相互学习,相互进步. 1.函数init <div id="one&q ...