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- ...
随机推荐
- Hibernate-细细道来-01
Dao代码,如何编写? 使用Jdbc技术,原始的jdbc操作, Connection/Statement/ResultSet DbUtils组件, 轻量级的dao的组件: Hibernate技术 [ ...
- 二级菜单jquery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 理解Servlet过滤器 (javax.servlet.Filter)
过滤器(Filter)的概念 过滤器位于客户端和web应用程序之间,用于检查和修改两者之间流过的请求和响应. 在请求到达Servlet/JSP之前,过滤器截获请求. 在响应送给客户端之前,过滤器截获响 ...
- bzoj2458: [BeiJing2011]最小三角形(分治+几何)
题目链接:bzoj2458: [BeiJing2011]最小三角形 学习推荐博客:分治法编程问题之最接近点对问题的算法分析 题解:先将所有点按x值排列,然后每次将当前区间[l,r]分成左右两半递归求解 ...
- ooize简介
在Hadoop中执行的任务有时候需要把多个Map/Reduce作业连接到一起,这样才能够达到目的.[1]在Hadoop生态圈中,有一种相对比较新的组件叫做Oozie[2],它让我们可以把多个Map/R ...
- vs版本与.net framework 版本对应
vs2002 .net framework 1.0 vs2003 版本号:7.x .net framework 1.1 window server 2003 vs2005 版本号:8.x . ...
- jQuery_pager.js分页
在做前端项目中,总是需要自己手写类似于这样的分页效果: 这就需要使用jQuery.pager.js文件,其使用方法为:在html中引入三个文件,分别为: <link rel="styl ...
- 关于call和apply的那点事儿
在JavaScript中改变闭包中的this关键字中经常用到的就是call和apply了 首先:call和apply的作用的区别是什么? 答:call和apply 的作用是相同的.都是用来改变函数th ...
- apache 日志为每个域名独立配置单独的日志文件
<VirtualHost *:80>DocumentRoot "E:\luyou\viplijiang"ServerName vip.li.comTransferLog ...
- CSS3卷角
众所周知,border-radius 属性可以用来设置圆角,但很少人知道它还可以做很多不规则的犄角.卷角(rounded corners) 工作原理: 一.独立属性:border-bottom-lef ...