leetcode 138. Copy List with Random Pointer(深copy带有random指针的链表)只遍历一遍

人工智能技术基础系列之:迁移学习 作者:禅与计算机程序设计艺术 1.背景介绍 随着深度学习和神经网络的快速发展,越来越多的应用场景需要处理大量的数据和复杂的任务。然而,由于计算资源、存储空间等条件的限制,传统的人工智能方法往往不能满足实际需求。因此,为了提高效率和降低成本,研究人员提出了迁移学习的概念。本篇文章将详细介绍迁移学习的基 阅读详情

A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.

Return the head of the copied linked list.

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

val: an integer representing Node.val
random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
Your code will only be given the head of the original linked list.

该题是深copy一个带有random指针的单向链表。

思路:
如果一个单向链表不带有random指针,正常就是不断创建新的node,一步一步到tail即可。

多一个random指针会有什么影响呢?比如刚创建到第2个node,random却指向第4个node,这时第4个node还没有创建出来;或者已经创建到了第6个node,而random却指向第1个,那怎么能确定第1个是已经存在的node还是需要重新创建呢。
你可能会说我用一个HashSet保存已经创建的节点,不就知道是不是已经存在了。但是创建新节点的时候我只知道节点的值,并不知道random指针指向的node,为什么呢,因为random指向的node可能还没有创建出来。后面一旦修改了random指针,node就不一样了还怎么去找是不是已经存在。

但是可以确定的是,已经给的head指向的链表是确定的,中间不会再变了,可以通过head链表的节点来找到深copy出来的新节点。那么这就需要建立一一映射,一个head节点(key)对应一个新copy出来的节点(value)。

可以按照没有random的步骤,head一步一步向右走,走一步创建一个新节点(深copy),修改它的next节点和random节点(存在就直接指,不存在创建新的)。

    public Node copyRandomList(Node head) {
        if(head == null) return head;
        
        HashMap<Node, Node> pair = new HashMap<>();
        Node newHead = new Node(head.val);
        pair.put(head, newHead);
        Node tmp = newHead;
        
        while(head != null) {
            if(head.next != null) {
                Node node = new Node(head.next.val);
                if(!pair.containsKey(head.next)) {               
                    pair.put(head.next, node);
                    tmp.next = node;         
                } else {
                    tmp.next = pair.get(head.next);
                }
                
            }
            
            if(head.random != null) {
                Node random = new Node(head.random.val);
                if(!pair.containsKey(head.random)){
                    pair.put(head.random, random);
                    tmp.random = random;
                } else {
                    tmp.random = pair.get(head.random);
                }
            }
            head = head.next;
            tmp = tmp.next;
        }
        return newHead;
    }
HiCMAE 论文复现:基于 RAVDESS 数据集的音视频情感识别 多模态情感识别是人工智能领域的重要研究方向,旨在通过结合多种感知模态(如音频、视频、文本等)来更准确地识别人类情感状态。与传统单模态方法相比,多模态方法能够利用不同模态间的互补信息,提高情感识别的鲁棒性和准确性。 RAVDESS(Ryerson Audio-Visual Database of Emotional Speech and Song)是一个常用的多模态情感识别数据集,包含24位专业演员(12名男性,12名女性)表达的8种情感状态(中性、平静、快乐、悲伤、愤怒、恐惧、厌恶和惊讶)的音视频数据。 阅读详情

相关推荐

Unity技能系统核心:从理论到实践构建可扩展的Buff管理器

本文深入探讨了Unity游戏开发中构建可扩展技能系统的核心——Buff管理器。通过剖析三层架构(数据、逻辑、渲染)与独立组件管理模式,提供了从Buff数据定义、管理器核心实现到可视化编辑器搭建的完整实践方案,旨在解决技能系统维护难题,提升开发效率与代码质量。

weixin_29002209的博客 263

#138. Copy List with Random Pointer

Description A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, wher

YY_Tina的博客 227

国民技术N32G45X SysTick定时器实战:从1us到100ms的精准延迟实现(附完整代码)

本文详细解析了国民技术N32G45X芯片中SysTick定时器的工作原理,并提供了从1微秒到100毫秒的高精度延迟实现方案。通过对比官方库函数的局限性,文章重点介绍了无中断查询模式、时钟源选择策略以及完整的代码实现,帮助嵌入式开发者精准控制时序,适用于LED控制、软件模拟通信协议等多种实战场景。

weixin_29190573的博客 388

138. Copy List with Random Pointer

题目: A linked list of lengthnis given such that each node contains an additional random pointer, which could point to any node in the list, ornull. Construct adeep copyof the list. The deep copy should consist of exactlynbrand newnodes, where each...

weixin_49991368的博客 191

优化问题-半正定规划(Semi-Definite Program, SDP)

半正定规划(Semi-Definite Program, SDP)在MIMO无线通信相关的信号处理中具有广泛的应用,在某些场景可以分析证明SDP的得到的解释原问题的全局最优化或者次优解。

qq_33668008的博客 1万+

东华大学2020考研计算机OJ题目解答分享——进阶篇(4)

4 一个月的天数 作者: Turbo时间限制: 1S章节: 分支结构 问题描述 : 输入年和月,输出该月有几天。 输入说明 : 输入两个整数,中间以空格分隔,第一个整数表示年,第二个整数表示月。 输出说明 : 输出该年该月的天数,输出时,行首与行尾均无空格,仅输出一个整数。 输入范例 : 2000 2 输出范例 : 29 解答思路: 闰年和质数出题一样频繁,简单必会 AC代码 #include...

qq_38344326的博客 481

leetcode刷题笔记(python3)--138. Copy List with Random Pointer

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. T...

weixin_44555304的博客 175

微信图片.dat文件解密实战:用Python一键转PNG(附完整代码)

本文详细介绍了如何通过Python解密微信图片.dat文件并转换为PNG格式的完整教程。文章深入解析了微信.dat文件的异或加密原理,提供了自动检测密钥的Python脚本,并分享了批量解密和性能优化的实用技巧,帮助用户轻松恢复和整理微信聊天图片。

weixin_29096377的博客 478

Copy List with Random Pointer -- LeetCode

原题链接:http://oj.leetcode.com/problems/copy-list-with-random-pointer/这是一道链表操作的题目,要求复制一个链表,不过链表的每个结点带有一个随机指针,指向某一个结点。我们先介绍一种比较直接的算法,思路是先按照复制一个正常链表的方式复制,复制的时候把复制的结点做一个HashMap,以旧结点为key,新节点为value。这么做的目的是为了第...

编程语言小筑 219

WSUS服务的部署

(3) 因为是测试环境,所以在“加入Microsoft更新改善计划”对话框中,取消“是的,我希望加入Microsoft 更新改善计划”复选框的选择,然后单击“下一步”按钮。(4)在 “选择服务器角色” 窗口中,选择“Windows Server更新服务”复选框,在弹出的添加所需功能对话框中单击“添加功能”按钮,单击“下一步”按钮。(6)在“连接到上游服务器”对话框中,单击“开始连接”按钮,系统将测试连接上游服务器可以下载信息列表,等一段时间后,下载更新信息完毕,单击“下一步”按钮。

2301_81844185的博客 2634

备战2025数学建模国赛(算法46):多目标优化

本篇博客将深入浅出地介绍多目标优化的核心概念、主流算法,并重点结合一个数学建模竞赛的经典案例,提供完整的Python代码实现,助你在未来的竞赛中游刃有余。它通过寻找帕累托最优解集,完美地诠释了“鱼与熊掌不可兼得”的权衡思想,为最终决策提供科学依据。这些问题都包含多个相互冲突的目标,传统的单目标优化方法(如梯度下降、单纯形法)对此无能为力,因为它们无法同时优化多个目标。在数学建模竞赛中,我们面临的绝大多数问题都不是单一的“最好”,而是充满权衡与妥协的“最优”。记住,成功的建模不仅在于解出问题,更在于清晰地。

YOLO 294

leetcode——138. Copy List with Random Pointer

描述 A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each ne

errly_liu2828的博客 175

组合预测模型 | LSTM-XGBoost长短期记忆神经网络结合极限梯度提升树时间序列预测(Matlab程序)

组合预测模型 | LSTM-XGBoost长短期记忆神经网络结合极限梯度提升树时间序列预测(Matlab完整程序)

前程算法屋的博客 2186

LeetCode 138. Copy List with Random Pointer

分析 难度 中 来源 https://leetcode.com/problems/copy-list-with-random-pointer/ 思路 Iterate the original list and duplicate each node. The duplicate of each node follows its original immediately. Iterate the ...

ov流岚vo的专栏 341

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode //C - 138. Copy List with Random Pointer

【代码】LeetCode //C - 138. Copy List with Random Pointer。

Made in Code 1316

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

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 to any node in the list or null. Return a deep copy of the list. The Linked List is represented in the input/output as a list of n nodes. Each node is re

qq_41878358的博客 318

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

leetcode138. Copy List with Random Pointer

新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。的拼接链表,如此便可在访问原节点的 random 指向节点的同时找到新对应新节点的 random 指向节点。利用哈希表的查询特点,考虑构建 原链表节点 和 新链表对应节点 的键值对映射关系,再遍历构建新链表各节点的 next 和 random 引用指向即可。给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

weixin_44245188的博客 953

【leetcode】(python)138. Copy List with Random Pointer使用随机指针复制链表

使用随机指针复制链表DescriptionExample题意解题思路code 138. Copy List with Random Pointer Medium Description A linked list is given such that each node contains an additional random pointer which could point to any n...

。 341

【leetcode】No.138 Copy List with Random Pointer

Copy List with Random Pointer My Submissions Question  Solution  Total Accepted: 50108 Total Submissions: 195575 Difficulty: Hard A linked list is given such that each node contai

五月花 588

LeetCode 138 Copy List with Random Pointer 复制带随即指针的链表 Java

例如原链表为 A->B->C,复制的节点分别为A',B',C',经过第一步后,原始链表将变为A->A'->B->B'->C->C'输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]深度拷贝一个带随即指针的链表,要求新链表内的所有指针不应指向旧链表的节点。输入: head = [[3,null],[3,0],[3,null]]输出:[[3,null],[3,0],[3,null]]

菜鸟小扎 687

LeetCode 138:随机链表复制(Copy List with Random Pointer)——思路解析 + 易错点总结

给定一个链表,每个节点包含三个字段:valnextrandom其中:------ "next":指向下一个节点 ---- "random":指向链表中的任意节点,或 "NULL"

2501_93577780的博客 424

Leetcode 138. Copy List with Random Pointer(python)

Leetcode 138. Copy List with Random Pointer题目解析解法:利用hashtable 题目 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.Retu...

qq_37821701的博客 497

LeetCode 138 Copy List with Random Pointer (链表)

A linked list of lengthnis given such that each node contains an additional random pointer, which could point to any node in the list, ornull. Construct adeep copyof the list. The deep copy should consist of exactlynbrand newnodes, where each new ...

Tc的专栏 259

【小白爬Leetcode138】1.7 复制带随机指针的链表 Copy List with Random Pointer

【小白爬Leetcode138】1.7 复制带随机指针的链表 Copy List with Random Pointer题目Description中文描述思路一 用map映射地址:序号思路二 map1映射地址:序号,map2映射序号:地址 Leetcode 138 medium\color{#FF7D00}{medium}medium 点击进入原题链接:LeetCode中国站 题目 做题之前: 如果对DeepCopy 深拷贝不理解的读者,需要补一下这方面的知识,或者看懂我的这句话: 一个变量 a 对应着一

weixin_41604040的博客 242

138. Copy List with Random Pointer**

138. Copy List with Random Pointer** https://leetcode.com/problems/copy-list-with-random-pointer/ 题目描述 A linked list is given such that each node contains an additional random pointer which could poin...

珍妮的选择的博客 260

LeetCode 138. Copy List with Random Pointer【链表,DFS,迭代,哈希表】中等

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。

memcpy0的博客 1233
上一篇: leetcode 2. Add Two Numbers(链表两数相加)
下一篇: leetcode 20. Valid Parentheses(有效括号)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址