Copy List with Random Pointer 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/copy-list-with-random-pointer/description/


Description

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.

Solution

/*
struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};
*/ class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (head == NULL)
return NULL;
RandomListNode *node1, *node2;
for (node1 = head; node1 != NULL; node1 = node1 -> next -> next) {
node2 = new RandomListNode(node1 -> label);
node2 -> next = node1 -> next;
node1 -> next = node2;
} RandomListNode* resNode = head -> next;
for (node1 = head; node1 != NULL; node1 = node1 -> next -> next) {
node2 = node1 -> next;
node2 -> random = node1 -> random ? node1 -> random -> next : NULL;
} for (node1 = head; node1 != NULL; node1 = node1 -> next) {
node2 = node1 -> next;
node1 -> next = node2 -> next;
node2 -> next = node2 -> next ? node2 -> next -> next : NULL;
}
return resNode;
}
};

解题描述

这道题目是关于链表深拷贝的变种。最难的一点就是不同于传统的链表,这道题中的链表每个节点会带有一个随机节点指针,指向链表中的任意一个节点。所以拷贝的时候,不仅要完成链表的顺序拷贝,还要完成在新的链表中随机节点指针的拷贝。

解题思路上,关键是如何保证新旧链表中的随机节点指针指向的节点的位置在新旧链表中是一样的。如果我们能够在进行随机节点的指向的复制的时候,知道新旧链表中当前节点还有当前节点指向的随机节点之间的一一对应关系,就可以完成随机指向关系的复制。

如图所示,如果要复制1号节点到3号节点的随机指向关系,需要我们知道新旧链表中,1号节点和3号节点的对应关系。

所以在进行相对简单的顺序拷贝之前,我们可以先考虑保存新旧链表中的节点一一对应的关系。这里可以采用一个做法:在顺序拷贝每一个节点之后,将节点插入到原来的链表中,相当于一个新旧链表的merge操作,在后期再进行原链表的恢复:

如图所示,这样我们在拷贝1号节点的随机指向时,就可以通过原链表1号节点指向的random节点的next节点找到新链表1号节点应该指向的对应节点。而在原链表中进行游标顺序移动的时候,只需要每一步多走一次next。后面节点的随机指向关系拷贝以此类推。

而链表的恢复也相对简单,不做赘述。

[Leetcode Week17]Copy List with Random Pointer的更多相关文章

  1. 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 ...

  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 拷贝带随机指针的链表

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

  4. 【leetcode】Copy List with Random Pointer (hard)

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

  5. 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 ...

  6. LeetCode _ Copy List with Random Pointer

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

  7. 【LeetCode】Copy List with Random Pointer

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

  8. leetcode 【 Copy List with Random Pointer 】 python 实现

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

  9. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

随机推荐

  1. Maven pom 文件解释

    1 - 什么是构建? 我们都知道,写完代码之后需要进行编译和运行,以笔者自身为例,使用 Eclipse 写完代码,需要进行编译,再生成 war 包,以便部署到 Tomcat. 在编写 Java 代码的 ...

  2. Kafka性能之道

    Kafka高性能之道 高效使用磁盘 零拷贝 批处理和压缩 Partition ISR 高效使用磁盘 >顺序写cipan >Append Only(数据不更新,无记录级的数据删除,只会整个s ...

  3. css3 字体渐变

    先看个效果 https://www.bienvillecapital.com/ 然后人家样式这样写的 font-family: Overpass,Helvetica,sans-serif; font- ...

  4. RT-thread 设备驱动组件之PIN设备

    在RT-thread 2.0.0正式版中引入了pin设备作为杂类设备,其设备驱动文件pin.c在rt-thread-2.0.1\components\drivers\misc中,主要用于操作芯片GPI ...

  5. [AHOI2009]最小割 最小割可行边&必须边

    ~~~题面~~~ 题解: 做这题的时候才知道有最小割可行边和必须边这种东西..... 1,最小割可行边, 意思就是最小割中可能出现的边. 充要条件: 1,满流 2,在残余网络中找不到x ---> ...

  6. BZOJ1833:[ZJOI2010]数字计数——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1833 https://www.luogu.org/problemnew/show/P2602 给定两 ...

  7. BZOJ Lydsy5月月赛 ADG题解

    题目链接 BZOJ5月月赛 题解 好弱啊QAQ只写出三题 A 判断多干个数乘积是否是某个数的倍数有很多方法,比较常用的是取模,但这里并不适用,因为模数不定 会发现数都比较小,所以我们可以考虑分解质因子 ...

  8. UVA.839 Not so Mobile ( 二叉树 DFS)

    UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...

  9. AOJ.综合训练.2016-12-8

    提示:多个题目的代码采用了C ++的写法,对应编译器选择G ++,请不要直接复制代码. 下周实验考试,GOOD LUCK! 感谢汪神提供E题C语言代码 所有题目已更新为C语言写法 所有题目已更新为C语 ...

  10. C++之内部类与外部类(嵌套类)及友元

    转载于:http://www.cnblogs.com/qzhforthelife/p/3226885.html 先上代码: class Outer { public: Outer(){m_outerI ...