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.

解题思路:

本题要求新建一个链表,使其完全等于输入链表,相当于对输入链表深复制。

题目的难点在于,原链表中有random指针,指向原链表对应的结点。当新链表建立时,需要对新结点的random指针赋值,使其指向新链表中的对应结点。这样就不能简单的复制地址,需要在新结点建立后,再找到对应正确的地址。暴力解法的时间复杂度为o(n2)。

o(n)的解法:

将每一个新结点,建立在旧结点的后面,形成:old1->new1->old2->new2->...oldN->newN->...;o(n)

建立后,重新遍历这个加长链表,new1->random = old1->random->next;o(n)

最后将新旧链表拆分;o(n)

最终时间复杂度为o(n),空间复杂度为o(1)

代码:

 /**
* Definition for singly-linked list with a random pointer.
* 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 head; RandomListNode* oldNode = head;
RandomListNode* nodeLeft = NULL;
RandomListNode* newNode = NULL;
RandomListNode* newList = NULL; while (oldNode) {
nodeLeft = oldNode->next;
oldNode->next = new RandomListNode(oldNode->label);
oldNode->next->next = nodeLeft;
oldNode = nodeLeft;
} oldNode = head;
newList = head->next; while (oldNode) {
newNode = oldNode->next;
if (oldNode->random == NULL)
newNode->random = NULL;
else
newNode->random = oldNode->random->next;
oldNode = newNode->next;
} oldNode = head;
while (oldNode) {
newNode = oldNode->next;
oldNode->next = newNode->next;
oldNode = oldNode->next;
if (oldNode)
newNode->next = oldNode->next;
} return newList;
}
};

【Leetcode】【Hard】Copy List with Random Pointer的更多相关文章

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

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

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. [Leetcode Week17]Copy List with Random Pointer

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

  6. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  7. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

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

  9. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

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

随机推荐

  1. Ubuntu 安装 Caffe

    Caffe Caffe 安装(Python2 CPU版本) 参考博文https://blog.csdn.net/pangyunsheng/article/details/79418896 安装环境 U ...

  2. 组件--Fragment(碎片)第二篇详解

    感觉之前看的还是不清楚,重新再研究了一次 Fragment常用的三个类: android.app.Fragment 主要用于定义Fragment android.app.FragmentManager ...

  3. 第7章 Scrapy突破反爬虫的限制

    7-1 爬虫和反爬的对抗过程以及策略 Ⅰ.爬虫和反爬虫基本概念 爬虫:自动获取网站数据的程序,关键是批量的获取. 反爬虫:使用技术手段防止爬虫程序的方法. 误伤:反爬虫技术将普通用户识别为爬虫,如果误 ...

  4. <深入理解JavaScript>学习笔记(2)_揭秘命名函数表达式

    写在前面的话 注:本文是拜读了 深入理解JavaScript 之后深有感悟,故做次笔记方便之后查看. 感觉这章的内容有点深奥....略难懂啊. 先坐下笔记,加深一下印象吧. 我主要记一下自己感觉有用的 ...

  5. zsh: command not found: gulp

    明明安装了gulp,但是为什么执行gulp命令却在控制台输出 zsh: command not found: gulp 可能因为gulp没有被全局安装 在控制台输入 which gulp 如果输出 g ...

  6. JAVA将数字钱数转换为大写

    1.Java文件的编写 package com.cwai.xtag; import java.util.Scanner; public class Num2Rmb { private String[] ...

  7. 一个JNI的helloworld小demo

    最近想学习一下jni,在网上看了一些demo,自己也操作了一遍,首先我将我自己学习的demo网站贴出来:https://blog.csdn.net/lwcloud/article/details/78 ...

  8. bnu 10805 矩形神码的 平面向量的运行

    矩形神码的 Time Limit: 1000ms Memory Limit: 65536KB Special Judge   64-bit integer IO format: %lld      J ...

  9. 数组的filter()方法

    filter()也是一个用的不多的方法,但有时候还是比较有用的: 首先,Array.filter()是数组的方法,它作为数组方法被调用,传入一个callback,返回Array中符合callback条 ...

  10. grunt-contrib-watch 监控 JS 文件改变来运行预定义的Tasks

    依赖于 GruntJs ~0.4.0 监控 JS 文件改变来运行预定义的Tasks Demo: watch: { scripts: { files: ['src/**/*.js'], tasks: [ ...