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. ZOJ 1115 Digital Roots

    原题链接 题目大意:给一个数字,每一位相加求和,不断重复过程,直到剩一位数字. 解法:考虑到输入的数字可以很大,把输入按照字符串格式读入,再逐位处理. 参考代码: #include <iostr ...

  2. ASP.NET Web API与Rest web api:发布到IIS(二)(同发布.NET webservice)

    本文档大部分来源于:http://www.cnblogs.com/zqzjs/p/4705994.html 工具VS2010,window环境win7 一:Webservice的创建与方法查看调用 1 ...

  3. tyvj1013 - 找啊找啊找GF ——二维背包变种

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1013 好吧,这题没节操=_= 状态f[u,v,i]表示:消费u的人民币和v的人品同时泡到i个mm所需要的 ...

  4. Socket实现异步通信

    private static void RecVing(IAsyncResult Result) {     //通过 result 获取socket.在这个socket上你启动了BeginAccep ...

  5. leetcode 141. Linked List Cycle ----- java

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  6. leetcode 119 Pascal's Triangle II ----- java

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  7. Kernel panic - not syncing: Attempted to kill init

    解决方法:系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜单,选择“kernel /vmlinuz-2.6.23.1-42.fc8 ro root=/dev/vogroup00/log ...

  8. 通过UserAgent判断智能手机(设备,Android,IOS)

    转:http://free0007.iteye.com/blog/2017329 /// 根据 Agent 判断是否是智能手机 ///</summary> ///<returns&g ...

  9. 学习ARM7、ARM9的操作系统选择经验! [转]

    一 首先说说ARM的发展        可以用一片大好来形容,翻开各个公司的网站,招聘里面嵌入式占据了大半工程师职位.广义的嵌入式无非几种:传统的什么51.AVR.PIC称做嵌入式微控制器:ARM是嵌 ...

  10. 复利计算器4.0 【java版】

    import java.util.Scanner; public class FuLi { public static void main(String[] args) { ; Scanner sca ...