[LeetCode]Copy List with Random Pointer
来源:互联网 发布:相似度矩阵 聚类算法 编辑:程序博客网 时间:2024/06/01 18:41
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.
/** * 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; map<RandomListNode*,RandomListNode*> m; RandomListNode* prehead = head; RandomListNode* ret = new RandomListNode(head->label); RandomListNode* pret = ret; while(head!=NULL){ m[head] = ret; if(head->next!=NULL) ret->next = new RandomListNode(head->next->label); ret = ret->next; head = head->next; } while(prehead!=NULL){ if(prehead->random==NULL){ (m[prehead])->random==NULL; } else (m[prehead])->random = m[prehead->random]; prehead = prehead->next; } return pret; }};
0 0
- [leetcode][list] Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode: Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- LeetCode题解:Copy List with Random Pointer
- [LeetCode]Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- leetcode-Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode | Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- LeetCode OJ:Copy List with Random Pointer
- LeetCode(138)Copy List with Random Pointer
- [LeetCode] - Copy List with Random Pointer
- DLX_HDU_2828
- Hbase配置项粗解(2)
- 【OpenCV入门教程之三】 图像的载入,显示和输出 一站式完全解析
- 输入图像四通道 输出图像四通道
- Android Callback浅谈
- [LeetCode]Copy List with Random Pointer
- HDU ACM 2548 两军交锋
- RFID原理与技术课程第八章作业
- window 与ubuntu共享文件 hgfs下为空和不用每次挂载hgfs的方法
- 黑马程序员——Foundation框架之结构体、字符串和集合
- 【OpenCV入门教程之四】 ROI区域图像叠加&初级图像混合 全剖析
- EXT - render 用法
- Win7与虚拟机VMware下运行的Ubuntu共享文件夹
- OpenTSDB安装——Ubuntu