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的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. Today is the first day of the rest of your life.

    Today is the first day of the rest of your life. 今天是你余下人生的第一天.

  2. Hibernate笔记6-JPA-注解

    一.JPA简介--Java Persistence API. 是SUN公司推出的一套基于ORM的规范.hibernate框架中提供了JPA的实现.JPA通过JDK5.0注解或XML描述对象-关系表的映 ...

  3. 【Linux/Ubuntu学习 11】git查看某个文件的修改历史

    有时候在比对代码时,看到某些改动,但不清楚这个改动的作者和原因,也不知道对应的BUG号,也就是说无从查到这些改动的具体原因了- [注]:某个文件的改动是有限次的,而且每次代码修改的提交都会有commi ...

  4. <logger>和<root>

    <logger>用来设置某一个包或者具体一个类的日志打印级别.以及制定<appender>.<logger>可以包含零个或者多个<appender-ref&g ...

  5. Unity中的输入

    目录 移动平台的输入 触摸 触摸相关的函数 触摸的一个示例 重力加速器 在Unity中访问重力加速器的信息 重力加速器示例 虚拟键盘 其他输入 传统的输入 鼠标,键盘,控制杆,手柄 虚拟控制轴(Vir ...

  6. CocoStudio UIButton setPressedActionEnabled(true) 子控件不跟着缩放

    具体情况是这样的:美术给了我 一个按钮的背景图片  一个按钮的文字图片,用背景图片创建一个button,然后把文字图片添加进去(注意关闭文字图片的交互功能) 设置UIButton setPressed ...

  7. DB2数据库常用语句

    1.快速清空大量数据表数据,但是还原不了 alter table rm_customer activate not logged initially with empty table2.大量导出表语句 ...

  8. LNA与PA

    LNA是低噪声放大器,主要用于接收电路设计中.因为接收电路中的信噪比通常是很低的,往往信号远小于噪声,通过放大器的时候,信号和噪声一起被放大的话非常不利于后续处理,这就要求放大器能够抑制噪声.PA(功 ...

  9. hihocoder 1109 堆优化的Prim算法

    题目链接:http://hihocoder.com/problemset/problem/1109 , 最小生成树 + 堆优化(优先队列). 可以用优先队列,也可以自己手动模拟堆,为了练手,我两种都试 ...

  10. java Vamei快速教程09 类数据和类方法

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们一直是为了产生对象而定义类(class)的.对象是具有功能的实体,而类是对象的 ...