Copy List with Random Pointer
来源:互联网 发布:15寸mac魔兽世界 编辑:程序博客网 时间:2024/06/06 15:44
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} * }; */
class Solution { public: RandomListNode *copyRandomList(RandomListNode *head) { if(head==NULL) return NULL; multimap<RandomListNode*,RandomListNode*>hmap; RandomListNode* p,*pre,*result,*temp=head;
//deep 拷贝next,建立多对一哈希表 while(temp){ p=new RandomListNode(temp->label); hmap.insert(make_pair(temp->random,p)); if(temp!=head){ pre->next=p; pre=pre->next; } else {pre=p;result=p; } temp=temp->next; } temp=head; pre=result;
//deep copy random 保证前向、后向也可以 while(temp){ if(hmap.find(temp)!=hmap.end()){ typedef multimap<RandomListNode*,RandomListNode*>::iterator iter; typedef pair<iter,iter> Range; Range range=hmap.equal_range(temp); for(iter i=range.first;i!=range.second;i++) i->second->random=pre; } temp=temp->next; pre=pre->next; } return result; } };
阅读全文
0 0
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- LSM树(Log-Structured Merge Tree)存储引擎
- WINDOWS10环境下apache2.4+php5.6.30(nts)+mysql5.7- web server环境手动配置
- Node.js 文件系统
- 欢迎使用CSDN-markdown编辑器
- 用百度地图api计算两个地方的距离
- Copy List with Random Pointer
- [leetcode]538. Convert BST to Greater Tree
- Leetcode053--找到分支最小路径和
- C++笔试题
- 每天一个 Linux 命令(41):ps命令
- C++中的inline关键字用法介绍
- (转)(笔记)screen tearing
- 宏定义函数和普通函数
- 设计模式---------单例模式