2014-05-01 01:32

题目链接

原题:

Given a linked list where apart from the next pointer, every node also has a pointer named random which can point to any other node in the linked list. Make a copy of the linked list.

题目:给定一个单链表,每个链表除了next指针以外,还有一个random指针,随机地指向nullptr或者链表中的某个节点。请设计算法完成一份链表的硬拷贝。

解法:Leetcode上有相关题目,请看题解LeetCode - Copy List with Random Pointer

代码:

 // http://www.careercup.com/question?id=5412018236424192
#include <unordered_map>
using namespace std; struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {};
}; class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int n; if(nullptr == head){
return nullptr;
} n = ;
RandomListNode *ptr; ptr = head;
while(ptr != nullptr){
++n;
mri[ptr] = n;
ptr = ptr->next;
} RandomListNode *root, *tail;
ptr = head; int i = ;
tail = root = nullptr;
while(ptr != nullptr){
++i;
if (root == nullptr) {
root = tail = new RandomListNode(ptr->label);
} else {
tail->next = new RandomListNode(ptr->label);
tail = tail->next;
}
mir[i] = tail;
ptr = ptr->next;
} RandomListNode *p1, *p2; p1 = head;
p2 = root;
while(p1 != nullptr){
if(p1->random != nullptr){
p2->random = mir[mri[p1->random]];
}
p1 = p1->next;
p2 = p2->next;
} mir.clear();
mri.clear(); return root;
}
private:
unordered_map<RandomListNode *, int> mri;
unordered_map<int, RandomListNode *> mir;
};

Careercup - Facebook面试题 - 5412018236424192的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. 根据不同的分辨率选择不同的css文件

    <SCRIPT language=javascript> <!-- Begin if (screen.width == 640) { document.write('<link ...

  2. Sql Server中的分组

    1.Group by可以将相同的数据合并为一组,分组后的数据可以看成是一个临时的表,注意如果sql中有where条件,那么group by必须放在where之后. 2.GROUP BY子句中可以指定多 ...

  3. MVC 增删改查

    MVC内部的增删改查语句都是用Lambda表达式写成的,因此要想使用MVC内部的增删改查就要学会写Lambda表达式. 这里拿表Train来仅作研究: //声明一个表Train的上下文对象 AirSy ...

  4. Cocos2d-js中使用纹理对象创建Sprite对象

    本节我们会通过一个实例介绍纹理对象创建Sprite对象使用,这个实例如图5-2所示,其中地面上的草是放在背景(如下图所示)中的,场景中的两棵树是从后图所示的“树”纹理图片中截取出来的,图5-5所示是树 ...

  5. TableView 中cell间的分割线(及其他控件间(内)的分割线)设置

    知识点: 1.在应用开发中,tableView中自带的分割线满足不了开发需求: 2.一些子空间中添加一些类似于分割线一样的东西等······ 补充知识点:导航条左侧或右侧的控件尽量靠近屏幕边缘设置 需 ...

  6. Java Executors(线程池)

    Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...

  7. css笔记——关于css中写上charset “utf-8”

    当css文件中写上 charset "utf-8" 时需要将body和html的样式分开写 例如: html,body{margin:0;padding:0;font-family ...

  8. c++之RTTI介绍

    本文介绍c++的RTTI的基本用法,并初步研究RTTI的实现原理. 1. 什么是RTTI RTTI即运行时类型识别(runtime type identification),用于判断指针或引用所绑定对 ...

  9. Windows phone(1)-ApplicationBar(应用栏)

    在手机APP应用当中,我们往往会想某些操作能够进行快速访问或者能够持续显示用户信息的情况,比如像做泡泡堂游戏右边区域玩家信息 在wp7,wp8中为我们提供了ApplicationBar这样的控件来创建 ...

  10. bootstrap时间插件 火狐不显示 完美解决方法

    原文链接:http://www.phpbiji.cn/article/index/id/141/cid/4.html bootstrap时间插件火狐 bootstrap-datetimepicker火 ...