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 ...
随机推荐
- 【Oracle】曾经的Oracle学习笔记(1-3) 数据库常见用语,常见命令,创建测试表
一.数据库的登录 二.数据库常用语 三.测试表的创建,测试数据初始化 四.常见命令介绍 五.测试 user:jeffreysn:jeffrey user:systemsn:jeffrey 浏览器中输入 ...
- 二、C++复数的实现
C++复数的实现 在数字图像处理领域,复数这一类型会被经常使用到.但是在C++和Qt中都没有可以使用的复数类.为了今后的方便,我们可以自己定义一个C++复数类,以便将来使用. 一.复数的属性 复数包含 ...
- linux 命令——31 /etc/group文件(转)
Linux /etc/group文件与/etc/passwd和/etc/shadow文件都是有关于系统管理员对用户和用户组管理时相关的文件. linux /etc/group文件是有关于系统管理员对用 ...
- 新建framework的bundle资源 图片资源被编译成了ttf后缀 解決
设置combine_hidpi_images为no
- IOS 某个控件出不来原因(经验分享)
某个控件出不来:(检查原因) 1.frame的尺寸和位置对不对 2.hidden是否为YES 3.有没有添加到父控件中 4.alpha 是否 < 0.01 5.被其他控件挡住了 6.父控件的前面 ...
- 【转载】LCT题单
本篇博客的题单转载自FlashHu大佬的博客:LCT总结--应用篇(附题单)(LCT). 关于\(LCT\)可以查看这篇博客:\(LCT\)入门. 这里面有些题解的链接是空链接,尚未补全. 维护链信息 ...
- Problem J: 搜索基础之红与黑
Problem J: 搜索基础之红与黑 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 170 Solved: 100[Submit][Status][ ...
- java设计模式——建造者模式
一. 定义与类型 定义:将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示 用户只需制定需要建造的类型就可以得到它们,建造过程以及细节不需要知道 类型:创建型 建造者模式与工厂模 ...
- Redis学习记录(三)
1.Redis集群的搭建 1.1redis-cluster架构图 架构细节: (1)所有的redis节点批次互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail ...
- windows下编辑器Emacs的安装与配置
一年成为Emacs高手(像神一样使用编辑器) http://blog.csdn.net/redguardtoo/article/details/7222501 原创作品,允许转载,转载时请务必以超 ...