[LeetCode] 132: Copy List with Random Pointer
2017-09-10 20:53
330 查看
[Problem]
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) {
// Note: The Solution object is instantiated only once and is reused by each test case.
// null node
if(NULL == head) return head;
// create new nodes of the old list
map<RandomListNode *, int> myMap; // stores the index of the nodes in the old list
vector<RandomListNode *> newNodes; // new nodes
RandomListNode *p = head;
int i = 0, j = 0;
while(p != NULL){
RandomListNode *node = new RandomListNode(p->label);
newNodes.push_back(node);
// stores index and increase index
myMap[p] = i++;
p = p->next;
}
// copy
p = head;
i = 0;
while(p != NULL){
if(p->next != NULL){
newNodes[i]->next = newNodes[i+1];
}
if(p->random != NULL){
newNodes[i]->random = newNodes[myMap[p->random]];
}
i++;
p = p->next;
}
return newNodes[0];
}
};
说明:版权所有,转载请注明出处。Coder007的博客
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) {
// Note: The Solution object is instantiated only once and is reused by each test case.
// null node
if(NULL == head) return head;
// create new nodes of the old list
map<RandomListNode *, int> myMap; // stores the index of the nodes in the old list
vector<RandomListNode *> newNodes; // new nodes
RandomListNode *p = head;
int i = 0, j = 0;
while(p != NULL){
RandomListNode *node = new RandomListNode(p->label);
newNodes.push_back(node);
// stores index and increase index
myMap[p] = i++;
p = p->next;
}
// copy
p = head;
i = 0;
while(p != NULL){
if(p->next != NULL){
newNodes[i]->next = newNodes[i+1];
}
if(p->random != NULL){
newNodes[i]->random = newNodes[myMap[p->random]];
}
i++;
p = p->next;
}
return newNodes[0];
}
};
说明:版权所有,转载请注明出处。Coder007的博客
相关文章推荐
- LeetCode:Copy List with Random Pointer
- LeetCode138: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 138 Copy List with Random Pointer
- leetcode之Copy List with Random Pointer
- Copy List with Random Pointer -- LeetCode
- leetcode---copy-list-with-random-pointer---链表
- LeetCode(Copy List with Random Pointer) 复杂链表的深拷贝
- [LeetCode][JavaScript]Copy List with Random Pointer
- leetcode第13题(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 @ Python
- leetcode: Copy List with Random Pointer