Deep copy and Shallow copy in Python
The original link of the leetcode challenge: https://leetcode.com/problems/copy-list-with-random-pointer/
浅拷贝:重新在堆中创建内存,拷贝前后对象的基本数据类型互不影响,但拷贝前后对象的引用类型因共享同一块内存,会相互影响。
深拷贝:从堆内存中开辟一个新的区域存放新对象,对对象中的子对象进行递归拷贝,拷贝前后的两个对象互不影响。
Deep copy: In python, this is implemented using “deepcopy()” function. In case of deep copy, a copy of object is copied in other object. It means that any changes made to a copy of object do not reflect in the original object.
138. Copy List with Random Pointer
import copy
class Solution:
def copyRandomList(self, head):
return copy.deepcopy(head)