LeetCode----Copy List with Random Pointer 深度拷贝,浅度拷贝,Lazy拷贝解析
题目: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.
分析:
题目给出了一个特殊的单链表。链表的每一个节点多了个指针域: random. 随机只向链表的某一个 node。
题目要求我们给出这个链表的一个 deep copy.
首先,何为 deep copy ??
Part 1: 三种 Object Copy 的对比(以 Array 为例)
1. Shallow copy
拷贝时仅仅复制的是 指针 或者 引用, 也就是说 数据仅存在一份。
当然,得到效率的同时,存在的问题就是,如果原来的数据改变了, 复制后的对象也改变了,因为仅仅存在一份数据!!!
Note: 其实是出于效率的考虑,在某些场合,并不需要多份数据时,可以采用 shallow copy
2. Deep copy
与 shallow copy 不同,deep copy 复制后,数据有多份。因此, deep copy 也比较费时。
在 C++ 中,可以自行定义类的 copy 构造函数来实现 deep copy.
Python 中,array 默认的复制是 shallow copy, 可以采用 copy 模块的 deep copy
3. Lazy copy
这是一种上述两种策略的组合。又称为 copy-on-write.
最初复制时,采用效率更高的 shallow copy, 同时用一个计数器 counter 记录当前share数据的对象数目。
当需要对数据进行修改时,根据 counter, 采用 deep-copy,
也就是当需要 deep-copy 时,才调用比较费时的 deep-copy.
回归本题:deep copy of the list
对于正常的单链表, 一份 deep copy 非常的容易。只要对链表循环一次,依次复制节点即可。
但是问题的困难在于: Node 多了一个 random 指针域。
对于 random 域:
如果 原链表: Node i 指向 Node j
新链表: Node i 同样指向 Node j(新链表的node)
如何能够找到新链表每个节点 random 域 所指向的节点呢??
思路: Step 1: 首先指向在原链表的每个节点后面,复制一个新的节点,原链表长度变为 2 倍
random 指针指向的是 原链表节点 random 指针指向的节点的后面的那个节点
Step 2: 将链表拆成两个 lists
/**
* 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 *tHead = head;
RandomListNode *next = NULL;
while(tHead)
{
next = tHead->next;
RandomListNode *node = new RandomListNode(tHead->label);
node->next = tHead->next;
//node->random = tHead->random;
tHead->next = node;
tHead= next;
}
tHead = head;
while(tHead)
{
if(tHead->random) tHead->next->random = tHead->random->next;
tHead = tHead->next->next;
}
RandomListNode *retHead = NULL;
RandomListNode *tRet = NULL; tHead = head;
RandomListNode *next2 = NULL;
while(tHead)
{
if(retHead == NULL)
{
next2 = tHead->next->next;
retHead = tHead->next;
tRet = retHead;
tHead->next = next2;
tHead = next2;
}
else
{
next2 = tHead->next->next;
tRet->next = tHead->next;
tHead->next = next2;
tHead = next2;
tRet = tRet->next;
} }
return retHead;
}
};
LeetCode----Copy List with Random Pointer 深度拷贝,浅度拷贝,Lazy拷贝解析的更多相关文章
- [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- Leetcode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- LeetCode——Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [leetcode]Copy List with Random Pointer @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- LeetCode – Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
随机推荐
- robotframework笔记14
创建用户关键字 关键字表是用于创建新的更高层次的关键词 结合现有的关键词. 这些关键字被称为 用户 关键字 区分他们的最低水平 库关键字 实现在测试库. 的语法创建用户 关键词非常接近的语法创建测试用 ...
- nodeschool.io 6
~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...
- (08)odoo继承机制
* 全局的引用 所有的的模型定义外,都在注册中心注册了,我们可以用全局变量来引用这些模型 self.env[mode name] 比如得到合作伙伴这个模型 self.evn['res.pa ...
- Unique Paths [LeetCode]
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- jquery iframe高度自适应
$(document).ready(function () { $("#test").load(function () { var thisheight = $(this).con ...
- 后Hadoop时代的大数据架构(转)
原文:http://zhuanlan.zhihu.com/donglaoshi/19962491 作者: 董飞 提到大数据分析平台,不得不说Hadoop系统,Hadoop到现在也超过10年 ...
- 关于时间序列数据库的思考——(1)运用hash文件(例如:RRD,Whisper) (2)运用LSM树来备份(例如:LevelDB,RocksDB,Cassandra) (3)运用B-树排序和k/v存储(例如:BoltDB,LMDB)
转自:http://0351slc.com/portal.php?mod=view&aid=12 近期网络上呈现了有关catena.benchmarking boltdb等时刻序列存储办法的介 ...
- EF 7 Code First
加载方式三种 1. Eager Loading 2. Lazy Loading 3.Explicit Loading 使用EF在与关系型数据库的交互中不可避免地需要加载数据,如何加载数据变得至关重要. ...
- 主机无法访问虚拟机上的elasticsearch服务器
问题: es在linux上搭建好,通过curl -XGET ip:port可以获得服务器信息展示,但是主机在浏览器上无法访问. 原因: 主机通过telnet访问linux的80端口,发现是不通的.可以 ...
- c# datagridview按条件搜索查询过滤
DataView的RowFilter 实现过滤 根据文本框文字对datagridview的数据进行模糊查询, 其实也就是一个过滤 string qymc = textBox1.Text.ToStrin ...