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. 网卡多ip 再看arp; arp队列也会缓存skb

    [结论] 当协议失效的时候,skb会挂载arp的neigt的一个链表上,然后直接返回了,相当于数据包发下了,当arp收到数据包去修复neigh的目的地址的时候,会把之前所有的neihe中等待的skb全 ...

  2. [Leetcode] 2.Add Two Numbers(List To Long,模拟)

    本题题意是指将两个数倒序存储在链表中,再将两数之和同样存储在链表中输出. 我最开始的思路是将每一位相加,再考虑是否进位,但这时就需要考虑一些情况,比较麻烦. 于是我决定采取另一种在网上新学到的方法:这 ...

  3. BZOJ 1005 明明的烦恼(prufer序列+高精度)

    有一种东西叫树的prufer序列,一个树的与一个prufer序列是一一对应的关系. 设有m个度数确定的点,这些点的度为dee[i],那么每个点在prufer序列中出现了dee[i]-1次. 由排列组合 ...

  4. vdbench-自动化测试脚本

    #!/usr/bin/python # -*- coding:utf8 -*- import sys import commands TEST_CONF=""" hd=d ...

  5. 编译 python 生成静态库 libpython2.7.so

    由于我们是C++作驱动的Python开发,驱动需要加上Python静态库libpython2.7.so.libpython2.7.so.1.0.libpython2.7.a.此处我想在python源码 ...

  6. WCF服务的建立以及调用

    WCF对我来说既陌生又熟悉,陌生是因为没怎么接触过,熟悉是听得太多,今天抽出点时间看了一下WCF,并且自己也写了一WCF的小程序以及调用WCF.步骤为: 1.创建一个解决方案WCF,和一个控制台项目W ...

  7. [SDOI2014][BZOJ3533] 向量集 [线段树+凸包]

    题面 BZOJ传送门 思路 首先当然是推式子 对于一个询问点$(x_0,y_0$和给定向量$(x_1,y_1)$来说,点积这么表达: $A=x_0x_1+y_0y_1$ 首先肯定是考虑大小关系:$x_ ...

  8. BZOJ4573:[ZJOI2016]大森林——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4573 https://www.luogu.org/problemnew/show/P3348#sub ...

  9. ACE中TCP通信

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/07/585095.html 概述: 传输控制协议TCP(Transmission Contro ...

  10. Naming Company CodeForces - 794C

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...