问题:

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.
* class RandomListNode {
*     int label;
*     RandomListNode next, random;
*     RandomListNode(int x) { this.label = x; }
* };
*/

分析:

原来的链表的结构如下图所示:

注意一点,random的指针可以指向后面的结点,也可以指向前面的结点。

 

这里提供两种解法:

 

方法1:用hash表存储结点信息,时间O(2n),空间O(n)

第一次遍历原链表,并构建random为null的新链表,于此同时在hash表中存储原结点和新结点的地址信息,原结点地址为key,新结点地址为value,即map<原结点地址,新结点地址>

第二次遍历原链表,对于random不为空的结点,可以根据random的值,在hash表中找到random指向的节点对应的新结点的地址,再以此给新结点random赋值即可。

 

方法2:先改变原链表的结构,在恢复,时间O(2n),空间O(1)

这个方法需要3次遍历,

第一次,构建新链表的结点,random为null,并且用原来链表节点的next指向对应的新结点,新结点的next指向原链表的下一个结点,如图所示:

 

第二次,给新节点的random赋值,

p = head;
while(p!=null){
if(p.random != null){
p.next.random = p.random.next;
}
p = p.next.next;
}

第三次,恢复原链表和新链表的链表结构。

head2 = head.next;
p = head;
while(p!=null){
p2 = p.next;
p.next = p2.next;
if (p2.next != null) p2.next = p2.next.next;
p = p.next;
}

 

 


 

方法2的完整代码:

public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)return null;
RandomListNode head2,p2,p = head;
while(p!=null){
RandomListNode n = new RandomListNode(p.label);
n.next = p.next;
p.next = n;
p = n.next;
}
p = head;
while(p!=null){
if(p.random != null){
p.next.random = p.random.next;
}
p = p.next.next;
} head2 = head.next;
p = head;
while(p!=null){
p2 = p.next;
p.next = p2.next;
if (p2.next != null) p2.next = p2.next.next;
p = p.next;
}
return head2;
}
}

LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)的更多相关文章

  1. [LeetCode] 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] Copy list with random pointer 对带有任意指针的链表深度拷贝

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

  4. 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现

    题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...

  5. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

  6. leetcode题解: Remove Duplicates from Sorted List(已排序单链表去重)

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

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

  8. leetcode 单链表相关题目汇总

      leetcode-19-Remove Nth From End of List—移除链表中倒数第n个元素 leetcode-21-Merge Two Sorted Lists—两个已排序链表归并 ...

  9. [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 ...

随机推荐

  1. 安装zookeeper遇到的问题以及解决方案

    伪分布式安装基本思想 zookeeper的安装包保存一份,但是zoo.cfg配置多份,启动zookeeper服务器的时候指定不同的zoo.cfg即可.即启动时这样启动:zkServer.sh star ...

  2. 【BZOJ-1340】Escape逃跑问题 最小割

    1340: [Baltic2007]Escape逃跑问题 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 264  Solved: 121[Submit] ...

  3. RAM、DRAM、SD卡

    catalogue . ROM.RAM.DRAM.SRAM和FLASH的区别 . 内存工作原理 . DRAM基本结构与原理 . SD卡基本结构与原理 1. ROM.RAM.DRAM.SRAM和FLAS ...

  4. js 数据类型 typeof的测试

    , t2 = ', t3 = null, t4 = NaN, t5 = undefined, t6 = function() {}, t7 = true, t8 = window, t9 = docu ...

  5. ruby

    :for 是关键字, each是方法. for 后面的变量,是全局变量,不仅仅存在于for .. end 这个作用域之内 module中的 self.xx方法可以被直接调用 module中的普通方法, ...

  6. elastichq auto connect

    $(document).ready(function () { $('#connectionURL').focus(); ajaxloading.hide(); scrollToTop.activat ...

  7. 大熊君大话NodeJS之------Connect中间件模块(第一季)

    一,开篇分析 截止到今天来说,NodeJS系列文章已经有将近十篇了,让我们回顾一下: (1),大熊君大话NodeJS之开篇------Why NodeJS(将Javascript进行到底) (2),大 ...

  8. Activiti学习(一) 环境搭建

    原料:Activiti5.4  MyEclipse 10 1.先将activiti文件夹放置myeclipse的安装目录dropins文件夹下2.将activiti文件夹里activiti.link中 ...

  9. mac 上的 python

    1.mac 上的 python 自己感觉很乱 1.额外安装的 自带的 python27-apple /System/Library/Frameworks/Python.framework/Versio ...

  10. PHP数组函数: array_walk()与 array_map() 的区别

    详细的介绍如下: PHP数组函数: array_walk() PHP数组函数: array_map() 实际应用中的一点区别与总结: array_walk() 主要用于对某个数组的迭代,相当于 for ...