Copy List with Random Pointer [LeetCode]
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.
- RandomListNode *copyRandomList(RandomListNode *head) {
- map<RandomListNode *, RandomListNode *> old_to_new;
- RandomListNode * current = head;
- RandomListNode * new_head = NULL;
- RandomListNode * pre = NULL;
- while(current != NULL) {
- RandomListNode * node = new RandomListNode(current->label);
- old_to_new[current] = node;
- if(new_head == NULL){
- new_head = node;
- pre = node;
- }else if(pre != NULL){
- pre->next = node;
- pre = pre->next;
- }
- current = current->next;
- }
- current = head;
- RandomListNode * new_current = new_head;
- while(current != NULL && new_current != NULL) {
- if(current->random != NULL)
- new_current->random = old_to_new[current->random];
- else
- new_current->random = NULL;
- current = current->next;
- new_current = new_current->next;
- }
- return new_head;
- }
Copy List with Random Pointer [LeetCode]的更多相关文章
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 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 ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- [leetcode]Copy List with Random Pointer @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
随机推荐
- django book querysets
from __future__ import unicode_literals from django.db import models from django.contrib.auth.models ...
- .Net分布式架构(二):基于Redis的Session共享
一:Session简介 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台web服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台web服务器建立连 ...
- Linux中的软硬链接
说到Linux中的软硬链接,就必须谈一下Linux的文件系统的组成的重要部分iNode和block. 首先是iNode,先用一张图了解一下iNode在Linux文件系统中的地位: Linux中的文件的 ...
- Java基本语法
一:跨行 Java变量不能跨行,如:String na me = “张三"; 字符串不能跨行,如:String a = "xxxxxxxxxx yyyyyyyy"; 二: ...
- windows系统调用 利用事件对象实现进程通信
#include "iostream" #include "windows.h" #include "cstring" using name ...
- cpp异常详解
1. 异常介绍 在函数在执行过程中如果碰到对错误的处理可以有两种方式, 1. 返回错误,2. 使用异常. 如果作为函数的调用者想要知道具体的错误信息, 就需要维护一套错误列表, 或者用string类型 ...
- Bug测试报告--连连看——天天向上
测试时间:2016-11-23 20:10 测试者:刘芳芳(nice!团队) 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git. ...
- HAL驱动库学习-SPI
如何使用SPI库1 声明SPI hanlde, 例如: SPI_HandleTypeDef hspi2 通过实现HAL_SPI_MspInit()函数初始化底层资源 以下两个必须进行初始化 a 使能s ...
- Android系统下,用adb实现自动获取应用性能数据
[自动化测试模式] 支持以adb shell命令的形式启动和运行.需要注意的是,office系列软件可能会更改命令中的字符,导致命令不可用!请手工输入命令,或从附带的command.txt文本中复制. ...
- [Effective JavaScript 笔记]第65条:不要在计算时阻塞事件队列
第61条解释了异步API怎样帮助我们防止一段程序阻塞应用程序的事件队列.使用下面代码,可以很容易使一个应用程序陷入泥潭. while(true){} 而且它并不需要一个无限循环来写一个缓慢的程序.代码 ...