Copy List with Random Pointer
来源:互联网 发布:windows vista重装 编辑:程序博客网 时间:2024/06/10 20:20
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
Solution:
/** * 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) { RandomListNode *res = new RandomListNode(-1); unordered_map<RandomListNode*, int> umRI; unordered_map<int, RandomListNode*> umIR; RandomListNode *p = head, *q = res; int num = 0; while(p) { RandomListNode* node = new RandomListNode(p->label); q->next = node; q = q->next; umIR[++num] = q; umRI[p] = num; p = p->next; } q->next = NULL; p = head; q = res->next; while(p) { if(!p->random) q->random = NULL; else { num = umRI[p->random]; q->random = umIR[num]; } p = p->next; q = q->next; } return res->next; }};
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
- UI_Menu_Options Menu
- 动态调用WebService的方法
- AT指令操作GPRS Modem实现网络传输
- 实现文字部分加粗
- Android之查看外部依赖jar的源码
- Copy List with Random Pointer
- Embedded Jetty+Spring MVC + Maven (IntelliJ 14) [三种简单启动方法 未完待续复杂配置]:
- C++字符串、向量和数组笔记
- Fragment进阶(二)----->动态、TAB页面切换
- mac os x 设置 jenkins开机启动遇到的问题
- Spring MVC 中的基于注解的 Controller
- 单例模式2-GCD实现单例模式
- TCP/IP网络编程 学习笔记_10 --套接字的多种可选项
- hdu 5371 Hotaru's problem