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 ...
随机推荐
- Linux下mysql编译安装
一.下载安装包 因为编译安装mysql需要cmake所以先编译安装cmake,这里都一起下载下来 下载cmake,网址https://cmake.org/download/ 下载mysql,网址htt ...
- ThinkPHP之中的图片上传操作
直接上个例子,其中包括有单图片文件上传.多图片文件上传.以及删除文件的一些操作.放置删除数据库的时候,仅仅删除掉了数据库之中的文件路径.而不是一并删除服务器之中的文件.放置服务器爆炸... TP里面c ...
- 怎样用foreach去修改数组之中的数据
$table_exchange=array(1,2,3,4,5,6,7,8); foreach ($table_exchange as $b=>$c){ $table_exchange[$b]= ...
- apache下ab.exe使用方法。。
自己在cmd中写了半天的路径也没有写对..最后网上的一个哥们告诉我说没有共同语言了...毛线啊 差距确实很大!大能猫死panda早晚干掉你,叫你丫整天嘲讽我! 比如我的ab.exe在D盘的wamp文件 ...
- python35
在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码.Python对bytes类型的数据用带b前缀的单引号或双引号表示x = b'ABC' //每个字 ...
- bat完美关机命令
@echo off title OLIVER-COMPUTER mode con cols= lines= color 0d shutdown /a >nul >nul ver > ...
- ZLG_GUI配置与函数介绍
http://www.docin.com/p-825479457.html ZLG_GUI配置与函数介绍
- supplicant
概述 wpa_supplicant是wifi客户端(client)加密认证工具,和iwconfig不同,wpa_supplicant支持wep.wpa.wpa2等完整的加密认证,而iwconfig只能 ...
- python小算法(二)
有两个序列a,b,大小都为n,序列元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小.(华为面试) def diff(sorted_list ...
- Objective-C 一些概念
Automatic Reference Counting (ARC)