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

HALCON卡尺模型实战:5分钟搞定工件尺寸测量(附完整代码) 本文详细介绍了HALCON卡尺模型在工件尺寸测量中的实战应用,通过5分钟快速搭建测量系统的完整指南,包括基础配置、高级参数调优和完整代码示例。卡尺模型能自动识别边缘并计算精确尺寸,显著提升工业视觉检测的效率和一致性,特别适用于实时测量场景。 阅读详情

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;
    }
AI驱动Spine骨骼动画生成:从图像拆分到动画自动化的全流程解析 计算机视觉与生成式AI技术正深度赋能传统数字内容生产流程。在2D动画制作领域,骨骼动画技术通过模拟真实生物关节运动,实现了角色动画的高效制作与灵活控制。其核心原理在于将角色拆分为独立部件,通过骨骼层级关系驱动部件变换,结合关键帧插值算法生成流畅动作。这项技术的工程价值在于大幅降低动画制作门槛,提升内容生产效率,尤其适用于游戏开发、影视预演、广告设计等需要快速原型迭代的场景。本文聚焦于如何利用SIFT特征匹配、RANSAC几何验证等CV算法实现部件自动对位,并结合动画12原则的程序化实现,构建从单张角色立绘到 阅读详情

相关推荐

模拟IC设计避坑:手把手教你用Cadence仿真恒定跨导轨到轨运放(附完整工艺库文件)

本文详细解析了使用Cadence仿真恒定跨导轨到轨运放的全流程,涵盖电路原理、工艺库配置、尺寸设计及仿真分析。通过1:3电流镜补偿技术实现跨导稳定,提供CSMC 0.25um工艺下的实战案例,帮助工程师规避设计陷阱,提升模拟IC设计效率。

weixin_30952535的博客 493

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

Win10插网线显示未识别网络的三种解决方法

本文介绍了三种解决Win10插网线显示未识别网络问题的方法:更新或重启网络设备驱动程序、重置Winsock目录、启用Network Setup Service服务。这些方法都是基于常见的网络问题和解决方案而设计的,适用于大多数Win10用户。如果你遇到了这一问题,可以按照上述步骤进行操作,相信一定能够找到解决问题的方法。需要注意的是,在进行任何操作之前,请务必备份重要数据以防万一。此外,如果你对计算机硬件或软件不熟悉,建议寻求专业人士的帮助以避免造成不必要的损失。

nntxthml的博客 6770

Copy List with Random Pointer -- LeetCode

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

编程语言小筑 219

电子产品设计中EFT防护的关键技术与实践

本文深入探讨了电子产品设计中EFT(快速瞬变脉冲群)防护的关键技术与实践。文章从EFT干扰的原理入手,系统阐述了通过优化PCB布局与地平面设计、构建电源与信号端口滤波防护电路、精选核心器件以及实施软件容错策略,来构建多层次、系统性的抗干扰防线,助力产品稳定通过严苛的电磁兼容测试。

e4f5g6h的博客 786

Loupe Browser实战指南:解锁10xGenomics单细胞数据的可视化奥秘

本文提供了Loupe Browser的实战指南,帮助用户无需编程即可探索和可视化10xGenomics单细胞数据。文章详细介绍了从数据导入、界面导航到细胞亚群鉴定、差异基因分析的核心操作流程,并分享了高级技巧与避坑指南,是快速上手这一强大可视化工具的必备参考。

weixin_29313423的博客 206

#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的博客 226

【NIPS 2022】时序精选论文07|Non-stationary Transformers: 引入非平稳性信息的平稳化序列预测模型(代码解读附源码)

整体代码框架基本上与传统 Transformer 类似,基本上只有在内嵌的注意力机制模块有区别,因此这里只对关键部分进行讲解,代码整体框架可参考 Transformer。非平稳时序很难预测,尤其对模型泛化性影响很大,试想下推理阶段的数据分布变了,模型很难适应这种变化。,比如很早以前的差分就是一种平稳化操作。序列平稳化能增强数据的可预测性,但这时也会带来一个问题,叫。,通过MLP层进行定义,使得参数可学习,以便于更好的融合到注意力网络中。代码中整体注意力的计算方式与原生Transformer类似,

ViolenceTemper的博客 1490

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的博客 173

Gitlab通过Webhook自动触发Jenkins构建任务并在更新完毕后发送钉钉消息通知

Gitlab通过Webhook自动触发Jenkins构建任务并在更新完毕后发送钉钉消息通知 文章目录Gitlab通过Webhook自动触发Jenkins构建任务并在更新完毕后发送钉钉消息通知1.实现思路2.新增钉钉机器人并测试消息的发送2.1.新增钉钉机器人2.2.测试发送消息到钉钉3.在Jenkins中新建一个构建任务3.1.新建任务3.2.配置任务时增加参数化构建过程3.3.配置任务添加git地址3.4.配置构建触发器生成gitlab webhook使用的token3.5.配置任务构建过程中执行的She

江晓龙的博客 3808

【目标检测】全局上下文感知的详解

目标检测中的全局上下文感知(Global Context Awareness)​是指模型在定位和识别目标时,能够有效利用图像中全局或大范围的上下文信息(如场景语义、目标间关系、空间布局等),而不仅仅依赖局部特征。这种能力显著提升了检测的鲁棒性,尤其是在复杂场景(如遮挡、小目标、密集目标)中表现突出。

浩瀚之水的专栏 643

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

YOLO-World:从入门到实战的多模态目标检测全指南

想象一下,你给模型一张“公园”的图片,再输入文字“红色的郁金香”,它能直接在图中框出所有红色郁金香的位置——这就是多模态目标检测:结合图像视觉信息和文本语义信息,同时完成“是什么”(分类)和“在哪里”(定位)的任务。传统目标检测(如YOLOv5、YOLOv8)只能识别预定义的类别(比如训练时学过“猫”“狗”),但多模态目标检测摆脱了“类别预定义”的限制,只要文本能描述,它就能检测,这就是“开放词汇检测(Open-Vocabulary Detection)”的魅力。

know_F0727的博客 204

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的专栏 340

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

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

Made in Code 1314

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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的博客 317

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

leetcode138. Copy List with Random Pointer

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

weixin_44245188的博客 951

【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

五月花 587

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的博客 422

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的博客 496

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的博客 240

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的博客 1229
上一篇: 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、付费专栏及课程。

余额充值