给大家推荐一道leetcode上的面试题,这道题的详细解说在《剑指offer》的P149页有思路解说。假设你手头有这本书。建议翻阅。

题目链接 here

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.

RandomList中。每一个节点不光有一个next指针。并且每一个链表节点都有一个random指针。随机指向链表中的一个节点,让你复制这个链表,节点的C++定义例如以下。

/**
* 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) {}
* };
*/

看到这个题目,预计非常多人的第一反应是,先把这个单链表复制出来,然后挨个找random指向的节点。细致想想这样效率非常低。复制链表非常快。但确定每一个节点的random指针就不easy了,用这样的方法的话。每找一个random指针,都必须遍历一次链表。时间复杂度O(n^2)。

可能也有人可能会想到时间换空间的方法,用hash把时间复杂度降到O(n)。

当然,还有更省时省空间的方法。

大概思路就是,在原链表的基础上,把每一个节点复制一份加的原来节点的后面,然后设置好新节点random指针,在把全部的新节点从原链表中分离出来。构成一个新链表,这个链表就是我们要的原链表的拷贝。

以下有三个函数,第一个明显就是复制新节点并把其增加到被复制节点的后面。第二个。由于新节点的random指针还是指向旧节点的。要把它指向新节点。非常easy,由于每一个节点的新节点都是在原来的节点之后的。

第三个函数。把新节点从原链表中抽离,构成一个新链表。

这样的方法的优点就是。时间复杂度仅仅有O(n),并且我们不须要额外的空间。

class Solution {
public:
void CloneNode(RandomListNode *head) {
RandomListNode * p = head;
while (NULL != p) {
RandomListNode * CloneNode = new RandomListNode(p->label);
CloneNode->next = p->next;
CloneNode->random = p->random;
p->next = CloneNode;
p = CloneNode->next;
}
} void ConnectRandomNode(RandomListNode * head) {
RandomListNode * p = head;
while (NULL != p) {
RandomListNode *pclone = p->next;
if (NULL != pclone->random) {
pclone->random = pclone->random->next;
}
p = pclone->next;
}
}
RandomListNode *copyRandomList(RandomListNode *head) {
if (NULL == head)
return head;
CloneNode(head);
ConnectRandomNode(head);
RandomListNode *pnode = head;
RandomListNode *pclonehead = pnode->next;
RandomListNode *pclonenode = pnode->next;
while (NULL != pnode) {
pnode->next = pclonenode->next;
pnode = pnode->next;
if (NULL != pnode){
pclonenode->next = pnode->next;
pclonenode = pclonenode->next;
}
}
return pclonehead;
} };

Leetcode Copy List with Random Pointer(面试题推荐)的更多相关文章

  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]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  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. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

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

  5. Leetcode Copy List with Random Pointer

    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 &Clone Graph 复杂链表的复制&图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  9. [Leetcode Week17]Copy List with Random Pointer

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

随机推荐

  1. SpringMVC访问静态页面

    Spring MVC显示静态页面 在前面搭建spring MVC环境时,我们设置了spring-mvc配置,通过tomcat来访问了index.jsp 页面,但是当我将页面换成.thml的静态面之后就 ...

  2. /etc/fstab 解析

    http://www.codesec.net/view/39930.html root@qs-wg-db1 /]# cat /etc/fstab LABEL=/ / ext3 defaults 1 1 ...

  3. arcgis Listview

    private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCrea ...

  4. Go:Hello World!

    备注 结束了一周紧张的工作,周末像品茶一样玩味一下Go,本文主要记录学习Go的经历. Go是什么? 官方网站:http://golang.org/. 在Windows下安装Go 官方教程:http:/ ...

  5. 【GISER&&Painter】Chapter01:WebGL渲染初体验

    基于上一篇OpenGL的渲染原理,这两周又陆续接触了一些关于WebGL绘图的一些内容,因为刚入门,很多东西又很晦涩,所以特意花了小半天的时间整理了一下,特此记录. 零  画一个多边形吧! 把一个多边形 ...

  6. [Android Memory] 使用 Eclipse Memory Analyzer 进行堆转储文件分析

    转载地址:http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-ma/index.html Eclipse Memory Analyzer ...

  7. 使用工具将安卓Android apk应用转换成Bar BlackBerry 10格式

    首先要强调的是命令行工具使用起来虽然比较方便,但是对使用者有一定要求.而使用在线的网页进行转换虽然步骤比较多,但是操作比较简单.所以,对于初步接触的同学来讲,建议还是使用上一篇博文的在线转换工具比较好 ...

  8. by,with

    一.表示使用有形的工具时,通常用with来表示.例如: 用钢笔写 write with a pen 用肉眼看 see with naked eyes 用锤子敲打 strike with a hamme ...

  9. java中 HashMap和Hashtable,list、set和map 的区别

    摘自: http://blog.chinaunix.net/uid-7374279-id-2057584.html HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Ma ...

  10. c语言打印uint64, int64

    http://stackoverflow.com/questions/9225567/how-to-print-a-int64-t-type-in-c