【LeetCode】138. Copy List with Random Pointer
题目:
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.
提示:
此题有两种方法,一种是按照原链表next的顺序依次创建节点,并处理好新链表的next指针,同时把原节点与新节点的对应关系保存到一个hash_map中,然后第二次循环将random指针处理好。这种方法的时间复杂度是O(n),空间复杂度也是O(n)。
第二种方法则是在原链表的每个节点之后插入一个新的节点,这样原节点与新节点的对应关系就已经明确了,因此不需要用hash_map保存,但是需要第三次循环将整个链表拆分成两个。这种方法的时间复杂度是O(n),空间复杂度是O(1)。
但是利用hash_map的方法具有其他的优点,如果在循环中加入一个判断,就可以检测出链表中是否有循环;而第二种方法则不行,会陷入死循环。
代码:
使用hash_map的方法:
/**
* 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) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head) return NULL;
unordered_map<RandomListNode*, RandomListNode*> mp;
// 创建一个新的链表头
RandomListNode *new_head = new RandomListNode(head->label);
// node1负责指向原链表,node2负责指向新链表
RandomListNode *node1 = head, *node2 = new_head;
/**
* 按照原链表的结构不断创建新的节点,并维护好next指针,将node1与node2的对应关系保存到hash_map中,
* 以备后面维护random指针的时候,可以通过node1找到对应的node2。
*/
while (node1->next != NULL) {
mp[node1] = node2;
node1 = node1->next;
node2->next = new RandomListNode(node1->label);
node2 = node2->next;
}
// 将两个链表的尾巴的对应关系也保存好
mp[node1] = node2; // 继续从头开始处理random指针
node1 = head;
node2 = new_head;
while (node1->next != NULL) {
node2->random = mp[node1->random];
node1 = node1->next;
node2 = node2->next;
}
// 把尾巴的random指针也处理好
node2->random = mp[node1->random];
return new_head;
}
};
不使用hash_map的方法:
/**
* 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) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
/**
* 假设:l1代表原链表中的节点;l2代表新链表中的节点
*/
RandomListNode *new_head, *l1, *l2;
if (head == NULL) return NULL; /**
* 第一步:在每一个l1后面创建一个l2,并让l1指向l2,l2指向下一个l1;
*/
for (l1 = head; l1 != NULL; l1 = l1->next->next) {
l2 = new RandomListNode(l1->label);
l2->next = l1->next;
l1->next = l2;
} /**
* 第二步:给l2的random赋值,l1的random的next指向的就是l2的random的目标;
*/
new_head = head->next;
for (l1 = head; l1 != NULL; l1 = l1->next->next) {
if (l1->random != NULL) l1->next->random = l1->random->next;
} /**
* 第三步:需要将整个链表拆成两个链表,具体做法是让l1的next指向后面的后面;
* l2的next也指向后面的后面。
*/
for (l1 = head; l1 != NULL; l1 = l1->next) {
l2 = l1->next;
l1->next = l2->next;
if (l2->next != NULL) l2->next = l2->next->next;
}
return new_head;
}
};
【LeetCode】138. Copy List with Random Pointer的更多相关文章
- 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告
题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...
- 【Lintcode】105.Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 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] 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 ...
随机推荐
- Linux与堆栈概念
在学习C/C++编程的时候,老师都会反复灌输一些概念.比如程序内变量在堆栈上的分配,栈是由高地址到低地址,堆是由低地址到高地址等等,然后画出这样一幅经典概念图: 图片来自:http://blog.cs ...
- HAProxy的三种不同类型配置方案
haproxy是一款功能强大.灵活好用反向代理软件,提供了高可用.负载均衡.后端服务器代理的功能,它在7层负载均衡方面的功能很强大(支持 cookie track, header rewrite等等) ...
- spring boot --- 初级体验
Spring boot的介绍我就不多说了,网上可以自己看一下. 它的优点就是:快!适合小白,没有复杂的配置文件. 缺点也很明显:坑有些多, 文档略少,报错有时不知道该如何处理. 下面做个最简单的入门: ...
- JVM与对象初始化
一个对象从无到有的过程 A a = new A() 1.JVM遇到new指令就会去堆内存分配一块内存空间,内存的大小在编译期间就可以确定 2.接着调用A的构造函数,这里构造的时候会沿着继承树逆流而上, ...
- struts2.1.6教程十二、总结
本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例. 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官 ...
- javaWeb学习总结(8)- JSP中的九个内置对象(4)
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...
- promise知识点汇总
Promise对象被写进ES6的规范当中,提供的是另外一种更加友好的对于异步编程的解决方案,在这之前大多使用的是回调函数和事件来实现异步编程. 怎么来理解Promise对象呢?对于这个ES6新加入的小 ...
- HDFS运行原理
HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Googl ...
- Running R jobs quickly on many machines(转)
As we demonstrated in “A gentle introduction to parallel computing in R” one of the great things abo ...
- vue+vux+axios+vuex+vue-router的项目的理解
本文主要是讲解项目前期的工作,后期考虑再详细说明. 作为一个技术团队如果你们团队选择了上面的技术栈,这说明你们的技术团体对于vue有很熟练的掌握了.在这里我想说明的是前期架构的重要.这里有一遍博客写的 ...