给大家推荐一道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. 利用站点ip引导提高站点权重的可行方案

    如题,利用站点每天高数额的ip訪问量来提高站点权重,首先在谈论这个话题之前,我举个样例.我们知道想要一个站点权重非常高,首先它站点本身的内容一定是有价值的,而且受大众欢迎的,人们会常常訪问这个站点来寻 ...

  2. lwIP Memory Management

    http://lwip.wikia.com/wiki/Lwipopts.h Memory management (RAM usage) /** * MEM_LIBC_MALLOC==1: Use ma ...

  3. apt-get和apt-cache命令实例展示

    示例:1 列出所有可用包 linux@localhost:~$ apt-cache pkgnamesaccount-plugin-yahoojpceph-fusedvd+rw-toolse3gnome ...

  4. arcgis Listview

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

  5. linux、mac的bash和zsh如何切换

    1.hostname 192-23-2-2 修改主机名字 2.chsh -s /bin/bash和chsh -s /bin/zsh可以永久切换,也就是一登录进来的就是相应的界面 bash/zsh命令是 ...

  6. BeagleBone折腾记(一):连接你的狗板

    BeagleBone折腾记一连接你的狗板 准备 了解BeagleBone BeagleBone社区 所需软硬件 USB连接 TTL连接 结语 准备 了解BeagleBone BeagleBone可能一 ...

  7. VC++多线程--进程间通信

    1.邮槽 邮槽是windows系统提供的一种单向通信的机制,邮槽能传输的数据非常小,一般在400k左右. 创建邮槽 HANDLE CreateMailslot( LPCTSTR lpName, //指 ...

  8. java设计模式5--原型模式(Prototype)

    本文地址:http://www.cnblogs.com/archimedes/p/java-prototype-pattern.html,转载请注明源地址. 原型模式 用原型实例指定创建对象的种类,并 ...

  9. IIs 中运行asp程序出现“An error occurred on the server when processing the URL. Please contact the system administrator.”错误

    在个人的win08系统上使用IIs运行 asp程序结果出现了以下错误 An error occurred on the server when processing the URL. Please c ...

  10. ORCU浅析之安装和作用

    众所周知,在安装Oracle BIEE之前需要安装Oracle RCU(Oracle Repository Creation Utility),美其名曰资料档案库.如果单单的从名称上来说,那就是我们实 ...