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. 第61天:json遍历和封装运动框架(多个属性)

    一.json 遍历  for in  关键字  for ( 变量 in  对象)  { 执行语句;  } 例如: var json = {width:200,height:300,left:50}co ...

  2. bzoj4278[ONTAK2015]Tasowanie & bzoj1692[USACO 2007Dec]队列变换(Best Cow Line) 贪心正确性证明

    做法网上到处都有就不说了. 这题其实是之前做的….不过由于人太傻现在才想明白比较字典序进行贪心的正确性…. 方便起见,在两个串的最右端都加上很大但不相同的字符,避免第lcp+1个字符不存在的边界. 如 ...

  3. lalala

    <script type="text/javascript"> var a_idx = 0; var b_idx = 0; var a = new Array(&quo ...

  4. 【luogu2181】对角线

    首先由于不会有三条对角线交于一点,所以过某一个交点有且只能有2条对角线 而这两条对角线实质上是确定了4个顶点(也可以看做是一个四边形的两条对角线交于一点,求四边形的数量). 因此我们只需要确定4个顶点 ...

  5. 用Docker搭建Nexus私服

    搜索Nexus 在docker容器中加载Nexus镜像 发布本地项目到Nexus私服 配置连接方式 发布指令 打源码包上传插件 搜索Nexus   在我们打算使用Nexus时,我们先搜索一下docke ...

  6. Android Bitmap和Drawable互转及使用BitmapFactory解析图片流

    一.Bitmap转Drawable Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp); 因为BtimapDrawable是Drawab ...

  7. 微软TTS语音引擎编程入门

    原文链接地址:http://www.jizhuomi.com/software/135.html   我们都使用过一些某某词霸的英语学习工具软件,它们大多都有朗读的功能,其实这就是利用的Windows ...

  8. 洛谷 P2323 [HNOI2006]公路修建问题 解题报告

    P2323 [HNOI2006]公路修建问题 题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 思路: 二分答案 然后把每条能加的大边都加上,然后加小边 但在洛谷的题 ...

  9. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  10. JSP页面中的Meta标签详解

    Meta标签详解 相信大家在平时开发中最常接触的页面就是html和jsp了,可在这两个页面中有一个Meta标签你天天都会看见,可是你真的了解这个标签的一些其他用处吗?今天给大家介绍一些该标签的其他应用 ...