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: Uses map to recorde node mapping between old linked list and new linked list.

  1. RandomListNode *copyRandomList(RandomListNode *head) {
  2. map<RandomListNode *, RandomListNode *> old_to_new;
  3. RandomListNode * current = head;
  4. RandomListNode * new_head = NULL;
  5. RandomListNode * pre = NULL;
  6. while(current != NULL) {
  7. RandomListNode * node = new RandomListNode(current->label);
  8. old_to_new[current] = node;
  9. if(new_head == NULL){
  10. new_head = node;
  11. pre = node;
  12. }else if(pre != NULL){
  13. pre->next = node;
  14. pre = pre->next;
  15. }
  16.  
  17. current = current->next;
  18. }
  19.  
  20. current = head;
  21. RandomListNode * new_current = new_head;
  22. while(current != NULL && new_current != NULL) {
  23. if(current->random != NULL)
  24. new_current->random = old_to_new[current->random];
  25. else
  26. new_current->random = NULL;
  27. current = current->next;
  28. new_current = new_current->next;
  29. }
  30. return new_head;
  31. }

Copy List with Random Pointer [LeetCode]的更多相关文章

  1. Copy List with Random Pointer leetcode java

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

  2. [Leetcode Week17]Copy List with Random Pointer

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

  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. 16. Copy List with Random Pointer

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

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

  6. LintCode - Copy List with Random Pointer

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

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

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

  8. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  9. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

随机推荐

  1. django book querysets

    from __future__ import unicode_literals from django.db import models from django.contrib.auth.models ...

  2. .Net分布式架构(二):基于Redis的Session共享

    一:Session简介 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台web服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台web服务器建立连 ...

  3. Linux中的软硬链接

    说到Linux中的软硬链接,就必须谈一下Linux的文件系统的组成的重要部分iNode和block. 首先是iNode,先用一张图了解一下iNode在Linux文件系统中的地位: Linux中的文件的 ...

  4. Java基本语法

    一:跨行 Java变量不能跨行,如:String na me = “张三"; 字符串不能跨行,如:String a = "xxxxxxxxxx yyyyyyyy"; 二: ...

  5. windows系统调用 利用事件对象实现进程通信

    #include "iostream" #include "windows.h" #include "cstring" using name ...

  6. cpp异常详解

    1. 异常介绍 在函数在执行过程中如果碰到对错误的处理可以有两种方式, 1. 返回错误,2. 使用异常. 如果作为函数的调用者想要知道具体的错误信息, 就需要维护一套错误列表, 或者用string类型 ...

  7. Bug测试报告--连连看——天天向上

    测试时间:2016-11-23 20:10 测试者:刘芳芳(nice!团队) 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git. ...

  8. HAL驱动库学习-SPI

    如何使用SPI库1 声明SPI hanlde, 例如: SPI_HandleTypeDef hspi2 通过实现HAL_SPI_MspInit()函数初始化底层资源 以下两个必须进行初始化 a 使能s ...

  9. Android系统下,用adb实现自动获取应用性能数据

    [自动化测试模式] 支持以adb shell命令的形式启动和运行.需要注意的是,office系列软件可能会更改命令中的字符,导致命令不可用!请手工输入命令,或从附带的command.txt文本中复制. ...

  10. [Effective JavaScript 笔记]第65条:不要在计算时阻塞事件队列

    第61条解释了异步API怎样帮助我们防止一段程序阻塞应用程序的事件队列.使用下面代码,可以很容易使一个应用程序陷入泥潭. while(true){} 而且它并不需要一个无限循环来写一个缓慢的程序.代码 ...