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. TCP/IP协议与OSI协议

    OSI协议是一个理想化的协议,它把网络传输过程分为七层模型,以达到形象化的理解的效果,在实际应用中没有被使用.TCP/IP协议可以看作是它的简化版,是目前应用最广泛的网络协议,许多协议都是以它为基础而 ...

  2. TDDL剖析

    前言 在开始讲解淘宝的TDDL(Taobao Distribute Data Layer)技术之前,请允许笔者先吐槽一番.首先要开喷的是淘宝的社区支持做的无比的烂,TaoCode开源社区上面,几乎从来 ...

  3. asp.net中缓存的使用

    刚学到asp.net怎么缓存,这里推荐学习一下 www.cnblogs.com/wang726zq/archive/2012/09/06/cache.html http://blog.csdn.net ...

  4. BZOJ 1296 粉刷匠(分组背包套DP)

    刚开始往网络流的方向想.建不出图... 因为每次只能对一行进行染色.每一行都是独立的. 对于每一行,因为格子只能染一次,所以可以发现这是一个多阶段决策问题,这个决策就是当前格子染0还是染1. 令dp[ ...

  5. poj 1018 Communication System (枚举)

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted:  ...

  6. 楼房 洛谷1382 && codevs2995

    P1382 楼房 题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平 ...

  7. Django+Celery+Redis实现异步任务(发送邮件)

    安装如下依赖库 pip install Celery pip install django-celery pip install django-redis 还要安装本地的Redis服务 setting ...

  8. python安装方法- 3.6.3版本

    一. 官网下载安装包: 官网网址:https://www.python.org/ 我下载的是3.6.3版本,如下图:  二. 安装安装包, 1. 直接双击运行  2. 选择Customize inst ...

  9. BZOJ3631:[JLOI2014]松鼠的新家——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3631 https://www.luogu.org/problemnew/show/P3258 松鼠的 ...

  10. BZOJ1087:[SCOI2005]互不侵犯——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1087 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王 ...