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.

/**
* 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) { }
};

============

这个链表节点和普通链表的区别就是,有一个random指针,可以指向链表中的任何元素或者为nullptr

返回产生一个深copy.

思路:

第一遍,对list的每一个节点复制一次放在节点后面,这一次只复制节点的next指针,不复制节点的random指针

第一遍结束后我们的链表节点

a->b->c->d...  会变成a->fake_a->b->fake_b->c->fake_c->d->d->fake_d....

第二遍再对链表进行random指针复制

第三遍堆链表进行拆分,这样的我们可以将链表分离出来一个新的链表.

code如下:

/**
* 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 *curr = head;
while(curr){
RandomListNode *node = new RandomListNode(curr->label);
node->next = curr->next;
curr->next = node;
curr = node->next;
}///a->f_a-> b->f_b-> ...
curr = head;
while(curr){
if(curr->random){
curr->next->random = curr->random->next;
}
curr = curr->next->next;
} ///partition it into two linktable
RandomListNode dummy(-);
RandomListNode *h = &dummy;
curr = head;
while(curr){
h->next = curr->next;
curr->next = curr->next->next;
h = h->next;
curr = curr->next;
}
h->next = nullptr;
return dummy.next;
}
};

138. Copy List with Random Pointer的更多相关文章

  1. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  2. [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  3. leetcode 138. Copy List with Random Pointer ----- java

    A linked list is given such that each node contains an additional random pointer which could point t ...

  4. 【LeetCode】138. Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  5. 138. Copy List with Random Pointer (Graph, Map; DFS)

    A linked list is given such that each node contains an additional random pointer which could point t ...

  6. Java for LeetCode 138 Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  7. 138. Copy List with Random Pointer (not do it by myself)

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. 138 Copy List with Random Pointer 复制带随机指针的链表

    给出一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点.返回一个深拷贝的链表. 详见:https://leetcode.com/problems/copy-list- ...

随机推荐

  1. 比较字符串,equals防空指针问题

    1,比较两个字符串内容的话,用a.equals(b)比较,其中a,b是两个字符串,用a==b的话比较的是a和b的内存地址.2,如果一个字符串是变量,另一个字符串是常量的话,一定要把常量写在前面,变量写 ...

  2. linux 解压命令大全

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------------- .gz 解压 ...

  3. 感知机-Python实现

    如图3所示的训练数据集,其正实例点是(3,3),(3,4),负实例点是(1,1),试用感知机学习算法的原始形式求感知机模型,即求出w和b.这里, 图3 这里我们取初值,取.具体问题解释不写了,求解的方 ...

  4. C语言的位运算

    位运算加速技巧1. 如果乘上一个2的倍数数值,可以改用左移运算(Left Shift) 加速 300% x = x * 2;x = x * 64;//改为:x = x << 1; // 2 ...

  5. C++ Primer : 第十二章 : 动态内存之shared_ptr与new的结合使用、智能指针异常

    shared_ptr和new结合使用 一个shared_ptr默认初始化为一个空指针.我们也可以使用new返回的指针来初始化一个shared_ptr: shared_ptr<double> ...

  6. html5的改进与沿革

    HTML5提供了一些新的元素和属性,例如<nav>(网站导航块)和<footer>.这种标签将有利于搜索引擎的索引整理,同时更好的帮助小屏幕装置和视障人士使用,除此之外,还为其 ...

  7. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  8. [JSOI2008] 完美的对称

    题目描述 在峰会期间,必须使用许多保镖保卫参加会议的各国代表.代表们除了由他自己的随身保镖保护外,组委会还指派了一些其他的特工和阻击手保护他们.为了使他们的工作卓有成效,使被保卫的人的安全尽可能得到保 ...

  9. 对CSS居中的一点总结

    在学习前端的过程中,发现元素和文本的水平居中和垂直居中,是经常会出现的问题,在实际工作中也会经常碰到.居中的技巧有很多,但在编写代码的过程中,发现有时候技巧管用,有时候不管用,于是就将每个知道的方案都 ...

  10. 【转载】关于Python中的yield

    在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...