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 ...
随机推荐
- IT技能等级
IT技能 低:会使用,会简单维修(操作) 中:能跟据实际业务需求扩展(技改.完善) 中高:找产品,能组合,能设计创造(出方案) 高:能规划(规划)
- cms-首页搭建
主页面主要有3个部分构成,头部.主体内容部分.尾部 1.头部: <%@ page language="java" contentType="text/html; c ...
- graylog日志收集过程举例
graylog的日志收集功与logslash类似,也是需要input-filter-output这样一个过程. 下面举三种最常用的日志记录来说明一下. 1,TCP报文日志 设置 ...
- vue+node+mongodb实现的页面
源代码地址:https://github.com/GainLoss/vue-node-mongodb 目前这个项目实现的是: 1.利用vue-cli实现前台页面的编写 (1)页面的跳转利用的是vue- ...
- linux 命令——ls
一. ls命令 ls 命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文 件及文件夹清单. 通过ls 命 ...
- linux 命令——35 ln(转)
ln 是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要 ...
- #linux 下Sublime的安装
1.Download http://www.sublimetext.com/2 Installtion use tar 解压压缩包,这里我将包改了个名字,这样就不用写空格的转义字符了,改成Subli ...
- Oracle 函数 之 wm_concat()
wm_concat() 把列转换成一行一列显示,使用wm_concat函数可以显示在一行一列. --1 建表 create table province_city ( province varchar ...
- angular2新建组件
1,使用ng g c hello 创建一个新的组件 它创建了4个文件,并更新了app.module.ts 如果想访问这个组件,只需要添加它的路由 成功访问这个组件 Import语句定义了我们需要用到的 ...
- 去除myeclipse中doget和dopost方法中的注释
当我们使用myeclipse新建servlet时发现doget和dopost方法中有一些无用的注释,每次新建一个servlet时都要手动删除特别麻烦. 下面就教大家如何去除这些注释! 以myeclip ...