原题地址

非常巧妙的方法,不需要用map,只需要O(1)的额外存储空间,分为3步:

1. 先复制链表,但是这个复制比较特殊,每个新复制的节点添加在原节点的后面,相当于"加塞"
2. 根据原节点的 ramdon 指针构造新节点的 random 指针
3. 恢复原链表结构,同时得到新复制链表

时间复杂度:O(n)

注意random有可能是NULL

代码:

 RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *h = NULL; h = head;
while (h) {
RandomListNode *node = new RandomListNode(h->label);
node->next = h->next;
h->next = node;
h = h->next->next;
} h = head;
while (h) {
h->next->random = h->random? h->random->next : NULL;
h = h->next->next;
} h = head? head->next : NULL;
while (head) {
RandomListNode *tmp = head->next;
head->next = head->next->next;
tmp->next = head->next ? head->next->next : NULL;
head = head->next;
} return h;
}

Leetcode#138 Copy List with Random Pointer的更多相关文章

  1. [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. Java for LeetCode 138 Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  3. [LeetCode] 138. 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 138. Copy List with Random Pointer ----- java

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

  6. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

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

  8. [Leetcode Week17]Copy List with Random Pointer

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

  9. 【LeetCode】138. Copy List with Random Pointer

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

随机推荐

  1. tomcat servlet 线程

    在服务器里,有一个servlet,当客户端第一次访问服务器时,tomcat会 帮我们建一个servlet的对象出来,(注意: tomcat里面可能部署了10个Servlet,如果某一个Servlet从 ...

  2. WIN8+VS2013编写发布WCF之三(调用)

    在文二中部署成功后就可以在客户端程序中使用服务了...使用服务的过程总是这么酣畅淋漓.当然,对应文二中的三种部署方式,我也会在此描述三种使用方式,一一对应. 都是新建个程序了,然后开始介绍. 一.VS ...

  3. MHA 日常管理

    一.MHA的主要脚本 1.manager端 masterha_check_ssh 检查MHA的SSH配置状况 masterha_check_repl 检查MySQL复制状况 masterha_mang ...

  4. ARM时钟初始化

    2440: S3C2440可以使用外部晶振(XTIpll)(默认为12MHZ)和外部时钟(EXTCLK)两种方式输入时钟信号.它由跳线OM[3:2]决定.S3C2440 默认的工作主频为12MHz(晶 ...

  5. [terry笔记]更改oracle用户名

    更改oracle的用户名 之前有个需求,整理一个schema的表.索引等规划到一个表空间里,利用expdp/impdp然后remap就完成了,但是整理好的用户名remap变更了,应用又不想修改其连接信 ...

  6. gem

    bundle gem xxxrake build gem push pkg/xxx.gem rake releaserake install

  7. NFC Forum : Frequently Asked Questions (NFC 论坛:FAQ)

    NFC for Business What is the NFC Forum? The NFC Forum is a not-for-profit industry organization whos ...

  8. 算法系列2《RSA》

    1. RSA介绍 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响 ...

  9. DWR在Spring中应用

    这里以传递一个对象为例,来说明dwr在Spring中是怎么配置的. JSP页面: <script src='dwr/interface/instructionOuterService.js'&g ...

  10. Linux: uid/euid/suid的关系

    三种进程用户的简单解释:三种用户/组ID:uid/gid: 实际用户/组IDeuid/egid: 有效用户/组ID, 进程执行某个应用的用户/组ID.suid/sgid: 设置用户/组ID, 应用所属 ...