LeetCode笔记】剑指 Offer 35. 复杂链表的复制(Java、哈希表、原地算法)
文章目录
- 题目描述
- 思路 && 代码
- 1. 哈希表法
- 2. 原地算法
- 二刷
题目描述
- 主要有两个考虑点:
- 不能改变原链表
- 新链表赋予 next、random 时,复制结点不一定存在
思路 && 代码
1. 哈希表法
- O(n)、O(n)
- 参考了dalao的写法,这里哈希表用得非常巧妙~值得学习!
- 思路:在哈希表中建立 Node - CopyNode 的联系,在此基础上进行 next && random 的处理即可。
/*
// Definition for a Node.
class Node {int val;Node next;Node random;public Node(int val) {this.val = val;this.next = null;this.random = null;}
}
*/
class Solution {public Node copyRandomList(Node head) {if(head == null) {return null;}// 哈希表做法<Node - CopyNode>Map<Node, Node> hashmap = new HashMap<>();for(Node temp = head; temp != null; temp = temp.next) {hashmap.put(temp, new Node(temp.val));}for(Node temp = head; temp != null; temp = temp.next) {// next 的处理hashmap.get(temp).next = hashmap.get(temp.next);// random 的处理hashmap.get(temp).random = hashmap.get(temp.random);}return hashmap.get(head);}
}
2. 原地算法
- O(n)、O(1),相对于方法1,此处不需要占用额外空间~
- 注意:原链表的恢复、边界结点的处理
class Solution {public Node copyRandomList(Node head) {if(head == null) {return null;}// 原地算法// 1. 首先 1 -> 2 -> 3 变成 1 -> 1* -> 2 -> 2* -> 3 -> 3*for(Node temp = head; temp != null; temp = temp.next.next) {Node copy = new Node(temp.val);copy.next = temp.next;temp.next = copy;}// 2. 进行 random 的处理for(Node temp = head; temp != null; temp = temp.next.next) {if(temp.random != null) {temp.next.random = temp.random.next;}}// 3. 分开 1 -> 1* -> 2 -> 2* -> 3 -> 3* 变成 1* -> 2* -> 3*Node copyHead = head.next;for(Node temp = head; temp != null; temp = temp.next) {// 取到原本的正确nextNode nextNode = temp.next.next;// * -> *if(nextNode != null) {temp.next.next = temp.next.next.next;}// 1 -> 2temp.next = nextNode;}return copyHead;}
}
二刷
- 哈希表
class Solution {public Node copyRandomList(Node head) {Map<Node, Node> map = new HashMap<>();for(Node temp = head; temp != null; temp = temp.next) {map.put(temp, new Node(temp.val));}for(Node temp = head; temp != null; temp = temp.next) {map.get(temp).next = map.get(temp.next);map.get(temp).random = map.get(temp.random);}return map.get(head);}
}
- 原地算法:二刷来看,还是很有含金量的做法思路
class Solution {public Node copyRandomList(Node head) {if(head == null) return null;for(Node temp = head; temp != null; temp = temp.next.next) {Node newNode = new Node(temp.val);newNode.next = temp.next;temp.next = newNode;}for(Node temp = head; temp != null; temp = temp.next.next) {temp.next.random = temp.random == null ? null : temp.random.next;}Node ans = head.next;for(Node temp = head; temp != null; temp = temp.next) {Node tempNode = temp.next;temp.next = temp.next.next;tempNode.next = temp.next == null ? null : tempNode.next.next;}return ans;}
}
LeetCode笔记】剑指 Offer 35. 复杂链表的复制(Java、哈希表、原地算法)相关推荐
- 【LeetCode】剑指 Offer 35. 复杂链表的复制
[LeetCode]剑指 Offer 35. 复杂链表的复制 文章目录 [LeetCode]剑指 Offer 35. 复杂链表的复制 package offer;import java.util.Ar ...
- 剑指 Offer 35. 复杂链表的复制(哈希/衍生拆分图解)
题目描述 请实现 copyRandomList 函数,复制一个复杂链表.在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null. ...
- 剑指 Offer 35. 复杂链表的复制
剑指 Offer 35. 复杂链表的复制 题目 题目链接 解题思路 题目理解 解题思路 具体代码 题目 题目链接 https://leetcode-cn.com/problems/fu-za-lian ...
- 【LeetCode 剑指 Offer 35. 复杂链表的复制(中等)】
题目: 请实现 copyRandomList 函数,复制一个复杂链表.在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null. ...
- 剑指offer——35复杂链表的复制
这题很是巧妙. 突破了常规思维. 竟然可以把传入进来的链表和复制的链表链在一起.然后再算出slibling指针.最后在分离. 直接把空间复杂度变为O(1)了. 很巧妙,很实用. 题目: 请实现函数Co ...
- 【三次优化】剑指 Offer 35. 复杂链表的复制
立志用最少代码做最高效的表达 请实现 copyRandomList 函数,复制一个复杂链表.在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意 ...
- leetcode 剑指 Offer 35. 复杂链表的复制
题意 将一个链表复制一遍后输出,这个链表除了next指针外,还有一个random指针,随机指向链表中的节点(包括自身或者NULL). 解法 生成一个新的链表,然后根据新老链表同时循环遍历,找到当前老链 ...
- 【算法】剑指 Offer 35. 复杂链表的复制 【重刷】
1.概述 地址:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/ 请实现 copyRandomList 函数,复制一个复 ...
- 【LeetCode】剑指 Offer 18. 删除链表的节点
[LeetCode]剑指 Offer 18. 删除链表的节点 文章目录 [LeetCode]剑指 Offer 18. 删除链表的节点 一.双指针 一.双指针 本题删除值为 val 的结点需分为两步: ...
最新文章
- 数量级提升!深度学习让机器人抓取更高效
- HACMP 认证学习系列,第 2 部分:计划与设计
- 新思路保障网络安全 基于平台的网络安全架构体系
- ipad和iphone切图_如何从iPhone和iPad上的Mail应用程序删除电子邮件帐户
- node中间件mysql_nodejs 中使用mysql数据有没有类似 mongoose 的中间件?
- struts2标签_select获取action传过来的值
- bat语句中“1>2”是什么意思?
- 所有科研人都应该收藏的论文下载网站,不是sci-hub!
- twitter数据集_推特宠物数据整理及分析
- 前端基础-CSS如何布局以及文档流,对于新手来说,特别有用
- django实现搜索功能
- 数控车床 刀尖补偿用法 G41 G42 G40
- 全国城市数据获取 mysql全国城市数据
- tensorboard 2.0可视化 —浏览器中输入http://ip:6006 - 无法访问此网站——有效解决
- ACPC Headquarters : AASTMT (Stairway to Heaven)
- 如何解决:ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
- Hebutgo 7.21 git使用(alicode)
- bzoj4605 崂山白花蛇草水 权值线段树套kd树
- PyQt+moviepy音视频剪辑实战2:一个剪裁视频文件精华内容留存工具的实现
- Thumbnailator实现图片压缩
热门文章
- Leetcode--845. 数组中的最长山脉
- oracle数据库的拼接字符串,Oracle数据库拼接字符串
- java 替换多个字符串_Java一次(或以最有效的方式)替换字符串中的多个不同子字符串...
- mysql load data infile 重写_mysql load data infile 命令的数据导入
- untitled软件怎么用_苹果手机怎么用4G网络于App Store下载超过200MB以上的软件
- C++ 传递字符串数组给函数参数
- 字符集和编码规范:ASCII,Unicode和UTF-8, latin1,BIG5,GBK
- PyTorch框架学习十六——正则化与Dropout
- windows下caffe+CPUOnly实现MNIST手写分类
- 利用gitbash上传项目到github