给大家推荐一道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. ida plug-in helloworld

    #include <ida.hpp> #include <idp.hpp> #include <loader.hpp> #include <kernwin.h ...

  2. .Net 加密 哈希

    一.DES加解密 DES一共就有4个参数参与运作:明文.密文.密钥.向量.其中这4者的关系可以理解为: 密文=明文+密钥+向量: 明文=密文-密钥-向量: 为什么要向量这个参数呢?因为如果有一篇文章, ...

  3. Supported_Hardware#4G_LTE_cards_and_modems

    https://wiki.mikrotik.com/wiki/Supported_Hardware#4G_LTE_cards_and_modems

  4. PHP 函数之 call_user_func &amp; call_user_func_array

    call_user_func_array (callable $callback, array $param_arr) 參数1: 调用一个回调函数, 參数2: 数组參数是回调函数的參数. call_u ...

  5. Java垃圾回收精粹 — Part4

    Java垃圾回收精粹分4个部分,本篇是第4部分.在第4部分里介绍了G1收集器.其他并发收集器以及垃圾收集监控和调优. Garbage First (G1) 收集器 G1 (-XX:+UseG1GC)收 ...

  6. MySQL -- 调优

    多数时候数据库会成为整个系统的瓶颈,比如大的数据量的插入与修改,频繁的亦或是高流量的访问,都会对数据库系统带来很大的压力.我在平时工作的时候,总是会遇到大数据量的插入.修改或是查询的操作,所以在工作的 ...

  7. HTML学习要点

    目标 掌握HTML基本语法,了解HTML Document结构,能熟练使用HTML Element对象. 要点 基本概念:什么是HTML.HTML标签? 熟悉常用的HTML标签含义以及应用场合. ht ...

  8. STM32+IAP方案 实现网络升级应用固件

    关注了这个概念有些日子了,这段时间总算有机会实战==网络升级应用固件,这里记录下遇到的问题,及解决方案. 原理与网上流传的串口作为传输手段 一致:不同之处,无非我这里使用了网络设备传输.==(lwip ...

  9. 大端和小端(Big endian and Little endian)

    一.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节):而 Little endian 则相反,它 ...

  10. JS-获取图片地址

    var url=document.getElementById("a001").src; 或var url=document.getElementById("a001&q ...