Careercup - Microsoft面试题 - 5917873302142976
2014-05-12 06:56
原题:
A link list contains following elements
struct node{
int data;
node* next;
node* random;
}
Given head of such a linked list write a function who copies such a linked list and returns the head of the new list. So if in the original list first node points to fourth node in random the copy should have the same relation. The random pointer can point to any node including itself and more than two nodes can have random pointer to the same node.
Required time complexity O(n) and no extra space can be used (apart from the newly allocated memory which you will need to create the new list)
题目:Leetcode上有这题,Copy List with Random Pointer。不过要求做到O(n)时间以及O(1)空间。
解法:之前我用了哈希表,能够实现O(n)时间,但不能做到O(1)空间。查阅了别人的解法后我发现自己的思路果然还不够灵活。这种解法很巧妙,先将新节点按照“旧->新->旧->新->旧->新”穿插到旧链表中,然后执行一条关键语句:ptr->next->random = ptr->random->next。执行完了以后将两条链表拆开即可(归并链表的逆过程)。哎,又是一个需要灵感才能想出的算法。
代码:
// http://www.careercup.com/question?id=5917873302142976
#include <iostream>
#include <unordered_map>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode*random;
ListNode(int _val = ): val(_val), next(nullptr), random(nullptr) {};
}; class Solution {
public:
ListNode *copyListWithRandomPointer(ListNode *head) {
if (head == nullptr) {
return nullptr;
}
ListNode *new_head;
ListNode *p1, *p2; p1 = head;
while (p1 != nullptr) {
p2 = new ListNode(p1->val);
p2->next = p1->next;
p1->next = p2;
p1 = p1->next->next;
} p1 = head;
while (p1 != nullptr) {
p1->next->random = p1->random == nullptr ? nullptr : p1->random->next;
p1 = p1->next->next;
}
new_head = splitList(head); return new_head;
};
private:
ListNode *splitList(ListNode *head) {
ListNode *head1, *head2;
ListNode *ptr1, *ptr2; // head1 is the original list.
head1 = ptr1 = nullptr;
// head2 is the new list.
head2 = ptr2 = nullptr;
while (true) {
if (head == nullptr) {
break;
}
if (head1 == nullptr) {
head1 = ptr1 = head;
} else {
ptr1->next = head;
ptr1 = ptr1->next;
}
head = head->next; if (head == nullptr) {
break;
}
if (head2 == nullptr) {
head2 = ptr2 = head;
} else {
ptr2->next = head;
ptr2 = ptr2->next;
}
head = head->next;
} return head2;
};
}; void deleteList(ListNode *&head)
{
ListNode *ptr; ptr = head;
while (head != ptr) {
ptr = head;
head = head->next;
delete ptr;
}
} int main()
{
int val;
int n;
int i;
ListNode *head1, *head2;
ListNode *ptr;
unordered_map<int, ListNode *> um;
Solution sol; while (cin >> n && n > ) {
head1 = head2 = nullptr;
for (i = ; i < n; ++i) {
cin >> val;
if (head1 == nullptr) {
head1 = ptr = new ListNode(val);
} else {
ptr->next = new ListNode(val);
ptr = ptr->next;
}
um[i] = ptr;
} ptr = head1;
for (i = ; i < n; ++i) {
cin >> val;
if (val >= ) {
ptr->random = um[val];
} else {
ptr->random = nullptr;
}
ptr = ptr->next;
} head2 = sol.copyListWithRandomPointer(head1);
ptr = head2;
while (ptr != nullptr) {
cout << ptr->val << ' ';
cout << (ptr->random != nullptr ? ptr->random->val : -) << endl;
ptr = ptr->next;
} deleteList(head1);
deleteList(head2);
} return ;
}
Careercup - Microsoft面试题 - 5917873302142976的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- Java集合框架—Map
Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 1,添加. put(K key, V value) putAll(Map<? extends K,? extends V& ...
- 用xaml画的带阴影3D感的圆球
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <E ...
- Eucalyptus常用查询命令
前言: Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- Notification高级技巧
观察Notification这个类,你会发现里面还有很多我们没有使用过的属性.先来看看sound这个属性吧,它可以在通知发出的时候播放一段音频,这样就能够更好地告知用户有通知到来.sound 这个属性 ...
- 使用Loadrunner监控Windows资源
为了区分把装有loadrunner的机器称作A,被监控资源的服务器(windows)称作B 1.确保B机器Administrator账户是可使用状态:右键计算机→ 管理→ 本地用户和组→ 用户,其中A ...
- word2013标题编号变成黑框
在使用word2013时,之前正常的标题编号有部分变成了黑框 解决方法: 1.将光标移动到标题中黑框右侧 2.按动键盘上的左方向键,直到黑框变成灰色 3.同时按键盘 Ctrl+Shift+S键,弹出“ ...
- ASP.net Session阻塞、Session锁、MVC Action请求阻塞问题
会话Session Session用于服务器端状态管理,使用Session之后,每个客户端都可以将实际的数据保存在服务器上,对于每个客户端的数据,将会生成一个对应的唯一的key(保存在客户端).客户端 ...
- TP5.0:的安装与配置
在网址中输入:localhost/安装TP5的文件夹/public/ 入口文件位置:public/index.php: 最新版本中,新建的文件夹是没有模型和视图的,需要自行添加没有的文件: 添加前: ...
- OpenCV转为灰度图像 & 访问像素方法
cvtColor(src, dst, CV_RGB2GRAY); 可转为灰度图像. 彩色图像像素访问:image.at<Vec3b>(i, j)[0],image.at<Vec3b& ...
- 「C基础」C运算符的优先级
一.运算符的优先级表 C 语言的符号众多,由这些符号又组合成了各种各样的运算符.既然是运算符就一定有其特定的优先级,下表就是C 语言运算符的优先级表: 注:同一优先级的运算符,运算次序由结合方向所决定 ...