LeetCode | Copy List with Random Pointer
2014-04-14 14:46
218 查看
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.
Shallow copy:
Some members of the copy may reference the same objects as the original.
Deep copy:
All members of the original are cloned. There are no shared objects.
主要区别在于拷贝之后指针成员指向的是不是同一个地址。
这道题最naive的方法就是用一个unordered_map来存original pointer和copied pointer的对应关系。空间复杂度o(n),时间复杂度o(n)。
比较smart的方法是,将copied node放在对应的original node后面,也就是original node1->copied node1->original node2->copied node2->....
然后就可以用p->random->next来求得random的新地址了。
最后再把copied list和original list分离开。
以后如果有题目需要把两个list对应起来的话,可以采用相同的方式,把它们串起来。
Return a deep copy of the list.
Shallow copy:
Some members of the copy may reference the same objects as the original.
Deep copy:
All members of the original are cloned. There are no shared objects.
主要区别在于拷贝之后指针成员指向的是不是同一个地址。
这道题最naive的方法就是用一个unordered_map来存original pointer和copied pointer的对应关系。空间复杂度o(n),时间复杂度o(n)。
class Solution { public: RandomListNode *copyRandomList(RandomListNode *head) { if (head == NULL) return NULL; unordered_map<RandomListNode*, RandomListNode*> pointerMap; RandomListNode *p = head, *copyH = NULL, *copyT = NULL, *tmp; while (p != NULL) { tmp = new RandomListNode(p->label); tmp->random = p->random; if (copyH == NULL) { copyH = tmp; } else { copyT->next = tmp; } copyT = tmp; pointerMap[p] = tmp; p = p->next; } p = copyH; while (p != NULL) { p->random = pointerMap[p->random]; p = p->next; } return copyH; } };
比较smart的方法是,将copied node放在对应的original node后面,也就是original node1->copied node1->original node2->copied node2->....
然后就可以用p->random->next来求得random的新地址了。
最后再把copied list和original list分离开。
以后如果有题目需要把两个list对应起来的话,可以采用相同的方式,把它们串起来。
RandomListNode *copyRandomList(RandomListNode *head) { if(head == NULL) return NULL; RandomListNode *p = head; do { RandomListNode *q = p->next; p->next = new RandomListNode(p->label); p->next->next = q; p = q; } while(p != NULL); p = head; do { p->next->random = (p->random == NULL) ? NULL : p->random->next; p = p->next->next; } while(p != NULL); p = head; RandomListNode *r = head->next; for(RandomListNode *q = r;;) { p->next = q->next; p = p->next; if(p == NULL) break; q->next = p->next; q = q->next; } return r; }
相关文章推荐
- leetcode 【 Copy List with Random Pointer 】 python 实现
- LeetCode: Copy List with Random Pointer
- Copy List with Random Pointer [LeetCode]
- [LeetCode] Copy List with Random Pointer
- leetcode_138 Copy List with Random Pointer
- LeetCode Copy List with Random Pointer
- leetcode:Copy List with Random Pointer 细致分析,以及代码实现(JAVA版本)
- [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 @ Python
- LeetCode: Copy List with Random Pointer
- Leetcode Copy List with Random Pointer 拷贝链表
- [Leetcode] Copy List with Random Pointer (Java)
- Copy List with Random Pointer--LeetCode
- Copy List with Random Pointer (LeetCode)
- [LeetCode] Copy List with Random Pointer
- LeetCode之Copy List with Random Pointer