复制带随机指针的链表 · Copy List with Random Pointer
[抄题]:
给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。
返回一个深拷贝的链表。
[思维问题]:
[一句话思路]:
完完全全地复制,否则不好操作。
1->1`->2->2`->3->3`->4->4` 紧随其后地复制,再拆开
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- newNode是每次新建的节点,不用往后移。head每次要移动2格,才能复制。
- head = head.next.next意在往下走,temp.next = temp.next.next;意在往下连
- if中非空的条件都是指针:if (head.next.random != null)
- RandomListNode newHead = new RandomListNode(head.label);表示有角标的从属关系
- split用的是快慢指针的思想,temp head分别连接即可(用temp是为了写法更简单):
while (head != null) {
RandomListNode temp = head.next;暂存
head = temp.next;连接head
if (temp.next != null) {连接temp
temp.next = temp.next.next;
}
} - 主函数里要写corner case :if (head == null) { return null; }
[二刷]:
- head.random.next是个数 = head.next.random是个指针;重点看最后一位
- splitList里面要用while循环,一直split
- head往后移动时,是head本身 = temp.next;
- 又不记得写corner case了,唉
[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
哈希彪
[Follow Up]:
[题目变变变]:
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
/**
* @param head: The head of linked list with a random pointer.
* @return: A new head of a deep copy of the list.
*/
private void copyNext(RandomListNode head) {
while (head != null) {
RandomListNode newHead = new RandomListNode(head.label);
newHead.next = head.next;
newHead.random = head.random;
head.next = newHead;
head = head.next.next;
}
} private void copyRandom(RandomListNode head) {
while (head != null) {
if (head.next.random != null) {
head.next.random = head.random.next;
}
head = head.next.next;
}
} private RandomListNode splitList(RandomListNode head) {
RandomListNode newHead = head.next;
while (head != null) {
RandomListNode temp = head.next;
head.next = temp.next;
head = head.next;
if (temp.next != null) {
temp.next = temp.next.next;
}
}
return newHead;
} public RandomListNode copyRandomList(RandomListNode head) {
copyNext(head);
copyRandom(head);
return splitList(head);
}
}
复制带随机指针的链表 · Copy List with Random Pointer的更多相关文章
- [Java]LeetCode138. 复制带随机指针的链表 | 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 no ...
- LintCode-105.复制带随机指针的链表
复制带随机指针的链表 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点. 返回一个深拷贝的链表. 挑战 可否使用O(1)的空间 标签 哈希表 链表 优步 code / ...
- Java实现 LeetCode 138 复制带随机指针的链表
138. 复制带随机指针的链表 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的 深拷贝. 我们用一个由 n 个节点组成的链表来表示输入/ ...
- 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现
题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...
- 138 Copy List with Random Pointer 复制带随机指针的链表
给出一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点.返回一个深拷贝的链表. 详见:https://leetcode.com/problems/copy-list- ...
- Leetcode138. Copy List with Random Pointer复制带随机指针的链表
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 方法一: class Solution { public: RandomLis ...
- 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...
- [LeetCode]138复制带随机指针的链表
题目描述: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 思路: 先遍历链表,将每个节点对应的随机指针指向的对象利用Hash ...
随机推荐
- [UE4]acotor放置4*4列表
// Number of blocks const int32 NumBlocks = Size * Size; // Loop to spawn each block ; BlockIndex< ...
- Linux命令详解-用户管理
1. 用户管理 1.Linux用户管理 linux有三类用户: (1.)超级用户 : root用户具有操作系统的一切权限 uid=0 (2.)普通用户: 具有操作系统有限的权限 uid=500-60 ...
- pandas的merge函数
pandas.merge(left,right,how='inner',on=None,left_on=None,right_on=None,left_index=False,right_index= ...
- javascript 中的函数声明和函数表达式区别
函数声明格式: function add(a, b) { alert(a+b); } 函数表达式格式: var add = function (a, b) { alert(a+b); } 解析器在向环 ...
- 深度强化学习:入门(Deep Reinforcement Learning: Scratching the surface)
RL的方案 两个主要对象:Agent和Environment Agent观察Environment,做出Action,这个Action会对Environment造成一定影响和改变,继而Agent会从新 ...
- SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法
我们在使用SpringBoot框架的时候在前面已经介绍了Thymelea引擎模板,因为SpringBoot对JSP惨不忍睹的支持.那我们在使用引擎模板对前端页面进行渲染能够返回的情况下我们怎么才能在静 ...
- string hashcode 解读
偶尔看到string hashcode方法如下 public int hashCode() { int h = hash; if (h == 0 && value.length > ...
- CentOS7.3下yum练手安装Nginx
安装Nginx # 查看相关信息 yum info nginx yum info httpd # 移除 httpd,也就是 Apache yum remove httpd -y # 安装 nginx ...
- 33. Linux安装配置JDK-7
安装说明 系统环境:centos-6.3安装方式:rpm安装 软件:jdk-7-linux-x64.rpm下载地址:http://www.oracle.com/technetwork/java/jav ...
- MFC-Dialog各函数的执行顺序
CDlgTestDlg::CDlgTestDlg CDlgTestDlg::DoModal CDialog::DoModal CDlgTestDlg::PreSubclassWindow CDlgTe ...