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. mp3转speex的一些研究(貌似不能播放,暂存着)

    思路是,先从mp3中提取pcm(raw原始数据),再将原始数据转成speex. 貌似不能播放,可能还存在其他问题,需要继续研究. 使用了两个类库NSpeex和NAudio using (var wav ...

  2. 【转发】Webdriver使用自定义Firefox Profile运行测试

    点击进入转发地址: 一般我们使用如下代码启动Firefox: 这样Selenium Server启动的Firefox将是一个全新的,不安装任何Add-On的Firefox. 如果有需要,我们可以使用我 ...

  3. Tomcat服务器安装配置

    1.到http://tomcat.apache.org/官网下载Tomcat的zip版本,这样直接解压就行了,不用安装.我下载是Tomcat6.0版本的zip文件,解压在D:\Java\apache- ...

  4. javac之Method Invocation Expressions

    15.12.1. Compile-Time Step 1: Determine Class or Interface to Search 15.12.2. Compile-Time Step 2: D ...

  5. ehcache 集群使用 rmi方式 有图有真想

    来源:http://www.tuicool.com/articles/MJzYZbR ehcache 有几种方式集群 ,rmi,jgroup还有jms:这里讲一下ehcache的使用 ehcache ...

  6. Bunder: What does :require => nil in Gemfile mean?

    https://stackoverflow.com/questions/12200215/bunder-what-does-require-nil-in-gemfile-mean Require ni ...

  7. jsp tomcat jdk版本对应

    jsp使用jdk8时,需要tomcat7以及以上版本,jsp在使用jdk7的时候,tomcat使用tomcat6即可

  8. Xml解析过程中遇到“” 1 字节的 UTF-8 序列的字节 1 无效“”问题---idea与eclipse同适用

    转载自:http://blog.csdn.net/zhangzhikaixinya/article/details/7727938 今天在eclipse中编写pom.xml文件时,识别到错误:Inva ...

  9. You have not concluded your merge. (MERGE_HEAD exists)。(转)

    自己简直就是一个git小白,碰到问题,一点点的解决吧,可能不太系统,但也只能勤能补拙了 Git本地有修改如何强制更新 本地有修改和提交,如何强制用远程的库更新更新.我尝试过用git pull -f,总 ...

  10. jQuery插件开发之windowScroll

    回首望,曾经洋洋得意的代码现在不忍直视.曾经看起来碉堡的效果现在也能稍微弄点出来.社会在往前发展,人也得向前迈进. 参考于搜狗浏览器4.2版本首页的上下滚动效果.主要实现整个窗口的上下和左右滚动逻辑, ...