LeetCode #138 - Copy List with Random Pointer

单细胞测序流程(十)KEGG通路富集分析与可视化圈图实战解析 本文详细解析了单细胞测序数据分析中KEGG通路富集分析与可视化圈图的完整实战流程。从差异表达基因列表出发,通过R语言进行KEGG富集分析,并利用Perl脚本进行基因ID转换,最终使用GOplot包绘制高级可视化圈图,帮助研究者将基因列表转化为有意义的生物学故事。 阅读详情

题目描述:

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.

/**
 * 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) {}
 * };
 */
// 用哈希表保存原链表和新链表中每个节点的对应关系,那么q->random = hash[p->random]
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if (!head) return NULL;
        RandomListNode *res = new RandomListNode(head->label);
        RandomListNode *p = head->next;
        RandomListNode *q = res;
        unordered_map<RandomListNode*, RandomListNode*> hash;
        hash[head] = res;
        while (p) {//这个循环中p比q早一步,方便复制一个和p一样的节点,让q指向它
            RandomListNode *tmp = new RandomListNode(p->label);
            q->next = tmp;
            hash[p] = tmp;
            q = q->next;
            p = p->next;
        }

        //这个循环中p和q同步
        p = head;
        q = res;
        while (q) {
            q->random = hash[p->random];
            q = q->next;
            p = p->next;
        }
        return res;
    }
};
// 将新链表穿插在原链表中,空间复杂度为O(1)
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head)
    {
        if (!head) return NULL;
        RandomListNode *p = head;
        while (p)
        {
            RandomListNode *new_node = new RandomListNode(p->label);
            new_node->next = p->next;
            p->next = new_node;
            p = new_node->next;
        }
        p = head;
        while (p)
        {
            if (p->random) p->next->random = p->random->next; //复制random指针
            p = p->next->next; //一次跳两步
        }
        p = head;
        RandomListNode *res = head->next;
        while (p)
        {
            RandomListNode * q = p->next;
            if(p->next) p->next = p->next->next;
            if(q->next) q->next = q->next->next;
            p = p->next;
        }
        return res;
    }
};

 

RM3100应用笔记驱动程序SPI接口 PNI磁传感器套件RM3100磁传感器套件由2个Sen-XY-f(pn13104)地磁传感器,1个Sen-Z-f(pn13101)地磁传感器和MagI2C(pn13156)控制芯片组成,能够实现三维空间的磁场大小测量。 阅读详情

相关推荐

NetApp 7-mode初始化详解(原AGGR重建)及注意事项

没想到有生之年还能再写个7-mode的文章,上次接触可能已将近十年,本次恰巧碰到这个机会,边学习边整理,整整一天时间,碰到了不少坑,作为记录。

RaySun的技术Blog 2760

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. The Linked List is represented in th...

zyp7355的博客 218

MoveIt2项目想换掉KDL求解器?试试这个trac_ik插件(附源码编译避坑指南)

本文详细介绍了如何在MoveIt2项目中用trac_ik插件替代KDL求解器,提升机械臂运动学求解的效率和成功率。内容涵盖环境准备、源码编译避坑、项目集成及性能调优,特别适合需要高精度控制的协作机械臂应用场景。通过实测数据对比,trac_ik在求解时间和奇异点处理上显著优于传统KDL求解器。

weixin_30730053的博客 707

Leetcode日练笔记35 [Linked List专题] #138 #61 Copy List with Random Pointer & Rotate List

Leetcode Linked List专题 #138 Copy List with Random Pointer & #61 Rotate List【Python解法】

Bittersweet_t的博客 137

Unity程序化河流生成工具Waterways:从算法到实践的全流程解析

程序化内容生成(PCG)是游戏开发与三维建模领域的关键技术,它通过算法自动创建游戏内容,显著提升开发效率。其核心原理是将设计规则转化为可执行的代码逻辑,从而生成复杂、多样且符合自然规律的环境资产。在开放世界游戏和模拟经营类项目中,程序化生成技术能够快速构建大规模、高一致性的自然场景,如山川、森林与水体网络。Unity引擎作为广泛使用的开发平台,其地形系统与着色器管线为程序化工具的集成提供了强大基础。本文聚焦于利用Waterways这一Unity工具集,深入探讨如何通过模拟重力驱动、流量累积等自然水力原理,在

weixin_33690963的博客 692

InternVL 3.5 深度解析:开源多模态模型的新里程碑

InternVL 3.5是由上海人工智能实验室OpenGVLab团队推出的开源多模态大模型,在推理能力、效率和灵活性方面取得突破。该模型采用ViT-MLP-LLM架构,包含60亿参数的InternViT-6B视觉编码器和多种规格的语言模型骨干。核心创新包括:1)Cascade RL强化学习框架,通过离线热身和在线强化两阶段训练提升推理能力;2)ViR视觉分辨率路由器,动态调整图像处理分辨率以优化计算效率;3)DvD动态视觉token压缩技术。模型系列涵盖2B到241B参数规模,满足从边缘设备到高性能计算的不

m0_47999117的博客 1041

【LeetCode】#138复制带随机指针的链表(Copy List with Random Pointer)

【LeetCode】#138复制带随机指针的链表(Copy List with Random Pointer) 题目描述 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。 要求返回这个链表的深度拷贝。 Description A linked list is given such that each node contains an additional ...

weixin_43858604的博客 138

doris2.1.8连接报错ERROR 1203 (42000): Reach limit of connections解决办法

摘要:客户Doris数据库报错"ERROR 1203 (42000): Reach limit of connections"的解决过程。发现AI提供的解决方案存在错误:1)查询命令应为SHOW FRONTEND CONFIG而非show variables;2)参数名称应为qe_max_connection而非max_connections。正确解决步骤:1)用admin账户登录查看连接数限制和当前连接数;2)通过SHOW PROPERTY命令检查用户资源限制;3)针对不同情况修改参数

Brent的专栏 1776

【leetcode】#哈希表【Python】138. Copy List with Random Pointer 复制带随机指针的链表

链接: https://leetcode-cn.com/problems/copy-list-with-random-pointer/ 题目: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深度拷贝。 解法1:先循环一遍,把node建完,把所有的node存在dic的key里;之后再循环一遍,把关系整好 class Solutio...

风轻扬逍遥子的博客 528

STM32 BLDC无刷电机霍尔信号捕获与换相控制实战解析

本文深入解析了STM32如何实现BLDC无刷电机的霍尔信号捕获与换相控制。详细阐述了硬件接口配置、定时器霍尔传感器模式的使用、抗干扰的滤波器与软件容错设计,以及中断服务与换相逻辑的实战代码。重点解决了信号抖动、换相不准等常见问题,旨在帮助开发者构建稳定可靠的电机驱动基础。

weixin_29193259的博客 373

华为OD机试双机位C卷:挑选宝石 (C/C++/Python/JAVA/JS/GO)

华为OD机试双机位C卷 挑选宝石,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解。华为OD机试-挑选宝石,华为OD机考-挑选宝石。

qq_45776114的博客 1507

leetcode138. Copy List with Random Pointer

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

weixin_44245188的博客 953

微信删除的聊天记录怎么恢复(2024年最新攻略合集)

随着微信对我们生活中方方面面的渗透,微信已然从一款简单的社交软件过渡成为现时代的一种生活方式,而微信中的聊天记录就是我们和朋友之间历程的最好证明。对此,今天我们来聊聊微信删除的聊天记录怎么恢复的问题。

m0_70960708的博客 955

LeetCode 138. Copy List with Random Pointer [Python]

LeetCode link: https://leetcode.com/problems/copy-list-with-random-pointer/ Question Description: A linked list is given such that each node contains an additional random pointer which could point to...

yfeng18的博客 161

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

[leetcode] 138. Copy List with Random Pointer @ python

原题 https://leetcode.com/problems/copy-list-with-random-pointer/ 解法 字典. 使用字典d, d的键是原节点, 值是复制的节点, 遍历链表两次, 第一次遍历是构造字典的键值对, 第二次遍历时, 将原节点的next和random复制到新节点中. Time: O(2*n) Space: O(n) 代码 """ # Definition fo...

闲庭信步 354

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

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

【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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Leetcode[138] Copy List with Random Pointer

an error:keyerror, class Solution(object): def copyRandomList(self, head): """ :type head: Node :rtype: Node """ if head==None: return None ...

只为分享 163

Leetcode 138. Copy List with Random Pointer

Problem 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 ea

小白菜又菜 251

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. 这个可以有两种方式去做,一个是使用正常的方式,使用map进行

ncst 297

138. Copy List with Random Pointer. Sol

LeetCode官方教程参考答案,穷学生只充了一个月的会员,记录下来备忘 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 exac...

这里是野生的高槻弥生~喵~ 137

[leetcode]138. Copy List with Random Pointer

138. Copy List with Random Pointer 方法一 Complexity O(n), Space O(n) 做一个旧结点和新结点的映射,再连接 random 指向的旧结点的映射结点 # Definition for singly-linked list with a random pointer. # class RandomListNode(object): # ...

CY_TEC的博客 248

Leetcode-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 point to any node in the list or

JamieLuo的博客 208

138. Copy List with Random Pointer

这里注意取的时候用.get 这样可以返回None。

ujn20161222的博客 198

【leetcode/python/138/M】Copy List with Random Pointer

对添加随意指针的链表进行深拷贝

alicelmx的博客 1369

leetcode 138. Copy List with Random Pointer复杂链表的复制

python代码如下: 1 # Definition for singly-linked list with a random pointer. 2 # class RandomListNode(object): 3 # def __init__(self, x): 4 # self.label = x 5 # ...

weixin_33971130的博客 150

leetcode 138. copy-list-with-random-pointer 复制带随机指针的链表 python3

时间:2020-7-9 题目地址:https://leetcode-cn.com/problems/copy-list-with-random-pointer/ 题目难度:Medium 题目描述: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。 要求返回这个链表的深拷贝。 我们用一个由n个节点组成的链表来表示输入/输出中的链表。每个节点用一个[val, random_index]表示: val:一个表示Node.val的整数。 ran...

isabloomingtree的博客 230
上一篇: LeetCode #135 - Candy
下一篇: LeetCode #157 - Read N Characters Given Read4
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值