题目

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.

分析

实现一个链表的深拷贝,返回拷贝后的新链表。

若是普通链表,逐个拷贝原始链表节点并连接即可,只需O(n)的时间复杂度;但是此题特殊的是,每个节点都有一个random域可以指向该链表中的任何一个节点,所以直接复制无法处理random域,因为其指向的节点很有可能还没有创建出来。

有两种方法处理:

方法一:暴力解决,首先不处理random域,将原始链表复制一份,然后遍历每个原始链表节点,查找其random域,将新链表的对应节点链接,该方法需要O(n^2)的时间复杂度,给出的结果是TLE。

方法二:充分利用原始链表的信息,不用保存原始链表的映射关系,构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面(参考):



同理分两步

1、构建新节点random指针:

new1->random = old1->random->next, new2-random = NULL,
new3-random = NULL, new4->random = old4->random->next

2、恢复原始链表以及构建新链表:

old1->next = old1->next->next,  new1->next = new1->next->next

该算法时间复杂度O(N),空间复杂度O(1)

AC代码

class Solution {
public:
//方法一:直接复制,再修改random指针
RandomListNode *copyRandomList1(RandomListNode *head) {
if (!head)
return NULL; RandomListNode *ret = new RandomListNode(head->label), *q = ret;
RandomListNode *p = head->next;
while (p)
{
RandomListNode *tmp = new RandomListNode(p->label);
q->next = tmp;
q = q->next; p = p->next;
}//while
q->next = NULL; //处理原始链表的random指针
p = head, q = ret;
RandomListNode *idx1 = head, *idx2 = ret;
while (p)
{
if (p->random == NULL)
q->random = NULL;
else{
idx1 = head;
idx2 = ret;
while (p->random->label != idx1->label)
{
idx1 = idx1->next;
idx2 = idx2->next;
}//while
q->random = idx2;
}//else
p = p->next;
q = q->next;
}//while
return ret;
} //方法二:充分利用原始链表信息,在每个节点后复制添加
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head)
return NULL; //首先,复制原始的节点,连接自身后面
RandomListNode *p = head;
while (p)
{
RandomListNode *tmp = new RandomListNode(p->label);
//保存后续节点
RandomListNode *r = p->next; tmp->next = r;
p->next = tmp; p = r;
}//while //然后,将添加的节点random 链接到原始节点random的下一个位置
p = head;
while (p)
{
RandomListNode *q = p->next;
if (p->random == NULL)
q->random = NULL;
else{
q->random = p->random->next;
}//else
//处理下一个原始节点
p = q->next;
}//while //最后,恢复原始链表,得到新链表
RandomListNode *ret = head->next; p = head;
RandomListNode *q = head->next;
while (q->next)
{
p->next = q->next;
p = q;
if (q->next)
q = q->next;
}
p->next = NULL;
q->next = NULL;
return ret;
} };

GitHub测试程序源码

LeetCode(138) Copy List with Random Pointer的更多相关文章

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

  2. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  3. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. dot watch

    dot watch+vs code提升asp.net core开发效率 在园子中,已经又前辈介绍过dotnet watch的用法,但是是基于asp.net core 1.0的较老版本来讲解的,在asp ...

  2. Nginx托管.Net Core应用程序

    Nginx托管.Net Core应用程序 一.安装.Net Core 参考官方文档:https://www.microsoft.com/net/core#linuxcentos 1.添加dotnet产 ...

  3. nginx错误Upstream timed out

    Upstream timed out (110: Connection timed out) while reading response header from upstream 这种情况主要在下面 ...

  4. Linq 根据list属性去重复

    s.Where((x, i) => s.FindIndex(z => z.ArticleTitle == x.ArticleTitle) == i).ToList();

  5. .net创建activex实现摄像头拍照

    using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices ...

  6. Reset CSS 页面初始化css

    CSS 初始化样式(Reset CSS 官网提供): /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: non ...

  7. 【简抄】h5 新增的几个背景属性和文本属性

    一.背景图像显示: ①background-size:规定背景图像的大小: 值:像素值.百分比.auto.cover.contain ②background-origin :规定背景图像的起始位置: ...

  8. kmem_alloc

    http://www.lehman.cuny.edu/cgi-bin/man-cgi?kmem_alloc+9

  9. 多段图动态规划dp

    多段图问题是DP的基础题目.大体的意思是有一个赋权有向图,其顶点集被分为几个子集.求经过每个子集从源点到终点的最短路径 import java.util.ArrayList; import java. ...

  10. cocos 2d-x 3.0配制环境

    cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...