Leetcode#138 Copy List with Random Pointer
非常巧妙的方法,不需要用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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
- [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表
public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...
- 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 ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- angularJS 数组更新时重新排序之解决方案一:这个坑,绕开吧,不跳了……
今天产品大人发现了一bug,图表数据和数据库总是对不上,原因是当前端更新数组时,angularJS默认对数组进行了排序. // 点击事件:input复选框 $scope.fnClickUpdateAr ...
- SequoiaDB(巨杉数据库)(社区版)安装配置使用图解
SequoaiDB是一款新型企业级分布式非关系型数据库,提供了基于PC服务器的大规模集群数据平台.作为全球第一家企业级文档式 NoSQL分布式数据库,为用户提供了一个高扩展性.高可用性.高性能.易维护 ...
- 新浪SAE URLRewrite(伪静态、重定向)详解
SAE全称Sina App Engine,真是一个好东西,他有很多优秀的特性,简单来说SAE就是一个简单高效的分布式Web服务开发.运行平台.支持现在常用的 PHP+Mysql 环境,在开发中难免会碰 ...
- 苹果Mac OS系统shell命令大全介绍
基本命令 1.列出文件 ls 参数 目录名 例: 看看驱动目录下有什么:ls /System/Library/Extensions 参数 -w 显示中文,-l 详细信息, -a 包括隐藏 ...
- SublimeText插件Emmet的自定义模板
在前端界,作为快速生成代码的Emmet插件相当给力.最近在学bootstrap,需要频繁生成html头文件,我就想着自定义模板.国内只有基础教程,只好自己读英文文档了. Emmet国内基础教程地址: ...
- Windows上搭建android开发环境
在搭建android开发环境时需要四部分内容,框架如下 其中Java SDK和Eclipse在java4android中有过介绍,重点介绍ADT和Android SDK的安装. 安装Android S ...
- Redis客户端之Spring整合Jedis
1.下载相关jar包,并引入工程: jedis-2.4.2.jar commons-pool2-2.0.jar 2.将以下XML配置引入spring <bean id="shard ...
- unix的策略与机制
策略同机制分离,接口同引擎分离 Linux/Unix设计理念提供的一种机制不是策略.如果说机制是一种框架,那么,策略就是填充框架的一个个具体实施.机制提供的就是一种开放而宽松的环境,而策略就是在这个环 ...
- EMVTag系列5《8E 持卡人验证方法(CVM)列表》
L: var. up to 252 -R(需求):数据必须存在,在读应用数据过程中,终端不检查 按照优先顺序列出卡片应用支持的所有持卡人验证方法 注:一个应用中可以有多个CVM列表,例如一个用于国内交 ...
- DB2中的转义字符
1.数据库脚本 )); ,'20%'); ,'OLIVER_QIN'); ,'AA''') 2.以下是DB2的转义字符 2.1 对“%”的转义 SELECT * FROM OLIVER_11 WHER ...