Careercup - Facebook面试题 - 5412018236424192
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的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- 日历控件table布局
作为初学者,一开始就接触div+css ,所以说实话,我并不怎么喜欢table布局,一般逃避. 先上这次的效果图: 看到这个图,第一次用table布局没实现,原因是给tr加下边框失效.当时没找到原因, ...
- 如何提高手机APP的用户体验?
详细内容请点击 随着移动互联网如日中天,如火如荼的时候,手机APP开发日益高涨了起来,关于手机APP的用户体验,也是一个老话长谈的话题.从事这行业也很久了,以下是我个人在工作中的一些关于APP的用户体 ...
- android菜鸟学习笔记2----关于adb
adb : android debug bridge android调试桥 路径:adt-bundle目录/sdk/platform-tools/adb.exe 常见的adb命令: adb devic ...
- js对象,原型,call,apply浅析
//对象直接量,创建对象最简单的方式是在js里使用对象直接量 var book = { "main title": "js", //属性里有空格,要用引号 &q ...
- 对App数据库元素进行简单的设计
假如对<豆瓣>进行简单的数据库元素设计; 分析页面: 简单的豆瓣一共有以下页面{ 活动页面 活动详情页面 电影页面 电影详情页面 影院页面(一般不用到数据库,不及于数据库考虑) 我的{ 活 ...
- IOS版应用商店应用源码
app商店 swift版 用swift编写的 应用商店 支持iPad iPhone利用了ios8过渡动画 支持横竖屏操作 源码下载: http://code.662p.com/view/11384.h ...
- DFS入门之一
深度优先搜索实现较为简单,需要控制两个因素: 1.已经访问过的元素不能再访问,在实际题目中还要加上不能访问的元素(障碍) 2.越界这种情况是不允许的 以杭电的1312 Red and Black 为例 ...
- Java web的读取Excel简单Demo
目录结构: Data.xls数据: 后台页面: GetExcelData.java public void doGet(HttpServletRequest request, Http ...
- OSX 上初步尝试 asp.net 5
去年微软最令开源狗界振奋的消息就是 ASP.NET 的开放与开源这一消息了. 根据微软的介绍,与我之前的一些总结,可以看到 ASP.NET 以及 .NET 平台会有下面几个变化: 代码使用 Apach ...
- git管理和自动部署项目
当一个项目需要纳入到版本控制的时候,选择的工具还是比较多的,最常见的就是工具有CVS,SVN,GIT等.在平时的开发中视情况而定,从来就没有最好的版本控制工具,只有最适合的工具.在这里我习惯用git来 ...