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. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  2. 用JavaScript访问SAP云平台上的服务遇到跨域问题该怎么办

    关于JavaScript的跨域问题(Cross Domain)的讨论, 网上有太多的资源了.国内的程序猿写了非常多的优秀文章,Jerry这里就不再重复了. 直入主题,最近我正在做一个原型开发:通过SA ...

  3. js 对象字面量

    对象字面量的输出方式以及定义好处 1.对象字面量的输出方式有两种:传统的'.' 例如:box.name 以及数组方式,只不过用数组方式输出时,方括号里面要用引号括起来 例如:box['name'] v ...

  4. [Rails学习之路]初识Ruby(二)

    继续上次Ruby的学习.接下来就到了Ruby的方法. Ruby的方法与Python仍然很像.使用def定义,可以使用undef取消定义. 在Ruby中,经常可以看见方法后面跟有"?" ...

  5. IOS截取部分图片

    截取部分图片这么简单: - (void)loadView {     [[UIApplication sharedApplication] setStatusBarHidden:YES withAni ...

  6. UVA 11600 Masud Rana(概率dp)

    当两个城市之间有安全的道路的时候,他们是互相可到达的,这种关系满足自反.对称和传递性, 因此是一个等价关系,在图论中就对应一个连通块. 在一个连通块中,当前点是那个并不影响往其他连通块的点连边,因此只 ...

  7. NOIP2018提高组Day1 解题报告

    前言 关于\(NOIP2018\),详见此博客:NOIP2018学军中学游记(11.09~11.11). 这次\(NOIP\ Day1\)的题目听说很简单(毕竟是三道原题),然而我\(T3\)依然悲剧 ...

  8. vuejs里面v-if,v-show和v-for

    <div id='root'> <div v-if='show'>helle world</div> <button @click='handleClick' ...

  9. php xdebug扩展无法进入断点问题

    Waiting for incoming connection with ide key 看到这句话就恶心 这两天搞php运行环境搞的头大,好在现在终于调通了,可以正常进入断点了 现在记录一下,避免下 ...

  10. 说说qwerty、dvorak、colemak三种键盘布局

    [qwerty布局] qwerty布局大家应该都很熟悉了,全世界最普及的键盘布局. 截止到去年接触并使用dvorak布局之前,我使用了十几年qwerty布局,在http://speedtest.10f ...