LeetCode: Copy List with Random Pointer

ArduSub深度调参指南:如何让水下机器人像鱼一样稳定游动 本文深入探讨了ArduSub水下机器人的深度调参策略,旨在解决ROV在水下晃动、响应迟钝等问题。文章系统解析了级联PID控制逻辑,并针对静水与水流环境,提供了具体的深度保持与姿态控制调参方法、实战日志分析技巧以及异常振荡处理方案,帮助用户实现水下机器人稳定、流畅的游动性能。 阅读详情

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.

耗时:104ms。


算法步骤:

  1.  将现有List复制一份到原有List, 即形成 A -> A' -> B -> B' -> .......的形式。 这样可以方便处理random节点的情况,假设A指向X, 如果分别创建两个链表,则需要O(N)的时间复杂度使得A' 指向 X'。但是如果建立上述结构,则可以在O(1)的时间内完成。即为:A->next->random = X->next;
  2. 将链表 A -> A' -> B -> B' -> .......断开,形成两条链表 A->B->... 和A'->B'->....

代码实现:
/**
 * 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) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        
        if(head == NULL)//exception
            return NULL;
        
        //copy all next node in the list   
        RandomListNode* t = head;
        while(t != NULL)
        {
            RandomListNode* tnext = t->next;
            RandomListNode* newt = new RandomListNode(t->label);
            t->next = newt;
            newt->next = tnext;
            t = tnext;
        }
        
        //copy all random node in the list 
        t = head;
        while(t!=NULL)
        {
            if(t->random != NULL)
            {
                t->next->random = t->random->next;   
            }
            t = t->next;
            t = t->next;
        }
        
        
        //spilt the list to result.
        RandomListNode* copyHead = head->next;;
        RandomListNode* copyt = copyHead;
        t = head;
        
        while(t!=NULL)
        {
            //delete t->next;
            t->next = t->next->next;
            if(copyt->next != NULL)
                 copyt->next = copyt->next->next;
            
            t = t->next;
            copyt = copyt->next;
        }
        
        return copyHead;
    }
};

cmu15445(2024fall) project 2 project 2比p1难度要更大,代码量更大且涉及的知识更多,实现中出现的bug更多,好在是最后全部解决了。我最大的收获在于对于多线程编程的理解更深入了,实际发现解决了多个多线程相关的bug,学会了更多多线程下debug的技巧方法,也见识了更多多线程的bug情况。在之后可以更系统的学习一下多线程编程。同时学会实现B+树也是需要掌握的基础。 阅读详情

相关推荐

STM32F4驱动ADS1220高精度ADC:从SPI配置到数据校准的完整实战指南

本文详细介绍了如何使用STM32F4微控制器驱动ADS1220高精度ADC,涵盖从SPI接口配置、寄存器设置到数据采集与校准的全过程。通过实战代码示例和硬件设计要点,帮助开发者快速构建高精度数据采集系统,适用于工业测量和精密仪器等应用场景。

weixin_26918763的博客 228

LeetCode:138. 随机链表的复制之如何有效copy

C语言实现:基因复制似的copy方法?这样运用单链表的插入?太神奇了!

nainaire的博客 730

AgentScope实战指南:从源码解析到Qwen模型的多Agent协作开发

本文提供了AgentScope多智能体开发框架的实战指南,详细解析了其核心架构与工作原理,并展示了如何利用Qwen模型构建从单智能体对话到多智能体协作的完整应用。通过两个具体代码实例,指导开发者快速上手环境配置、模型接入与智能体编排,实现高效的多Agent系统开发。

ff678的博客 642

LeetCode—随机链表的复制(深拷贝)

1.将拷贝节点插入到原节点后面2.深拷贝3.将新节点拿下来尾插形成新链表,恢复原链表。

2401_83431652的博客 502

ZYNQ新手必看:5分钟搞定SD卡与QSPI Flash双启动模式配置(附常见问题排查)

本文为ZYNQ开发者提供了SD卡与QSPI Flash双启动模式的完整配置指南与深度排错方法。详细解析了从BootROM、FSBL到BOOT.bin的启动链条,并分步讲解了在Vivado/Vitis中的硬件配置、软件工程创建及BOOT.bin文件生成流程。针对SD卡格式化、文件放置以及QSPI Flash烧录中的常见陷阱(如版本差异、时钟配置)给出了具体解决方案和排查清单,帮助开发者快速实现可靠的产品启动与程序固化。

ll5678的博客 939

海思图像质量之HiPQTool调试

文章目录一. HiPQTool 界面二. 图像调试三. 将调试参数写入程序中 一. HiPQTool 界面 二. 图像调试 三. 将调试参数写入程序中

lzg2011的博客 2389

leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)

LC 复制带随机指针的链表题目解题方法一:直接deepcopy方法二:先找到所有要复制的节点,再连线。 题目 题目链接 给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。 构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向

菊头蝙蝠的博客 574

【PostgreSQL教程】PostgreSQL 高级篇之子查询

子查询也可称为内部查询、嵌套查询,指的是在 PostgreSQL 查询中的 WHERE 子句中嵌入查询语句。一个 SELECT 语句的查询结果能够作为另一个语句的输入值。子查询可以与 SELECT、INSERT、UPDATE 和 DELETE 语句一起使用,并可使用运算符如 =、、>=、

No8g攻城狮的博客 1211

【LeetCode】复杂链表的复制

题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空) 思路: 对于复杂链表,一般形式会在next域的基础上增加一个random域,这个指针会指向任意的结点,当然,其也可以指向空指针域,其一般结点的构造形式为...

码农印象 2348

滤波算法实现大全——基于匀速直线运动模型的kalman滤波算法

滤波算法实现大全——基于匀速直线运动模型的kalman滤波算法

jane0819的博客 529

如何学习总线技术

如何学习总线技术 以形象生动的比喻来描绘了总线技术的基本思想,指出了总线的基本分类和总线传输的基本原理,以及在学习过程中应当掌握的最基本的知识,对初学者有起到抛砖引入的作用。 0引言   如果一座只能容一个人来往的独木桥,两端的人都想要过桥,为了不拥挤、阻塞,那我们就得采取有效的办法。比如规定某段时间哪端的人过桥,另一端的人就等着该他过桥的时间段的到来,同时也还可以规定人多时要按先来后到或年

ce123的技术博客 8531

Leetcode 每日一题:Copy List with Random Pointer

Google 高频面试题:Leetcode 139:Copy List with random Pointer 题目详解

2401_85421495的博客 1372

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

leetcode: Copy List with Random Pointer

Copy List with Random Pointer  Total Accepted: 18999 Total Submissions: 81709My Submissions A linked list is given such that each node contains an additional random pointer which could poi

qjklw 697

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode题解:Copy List with Random Pointer

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

Magi Su的技术回收站 1349

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[LeetCode] 132: Copy List with Random Pointer

[Problem] 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. [Solution]/** * Defini

1.02^365的成长裂变 3719

Leetcode:Copy List with Random Pointer

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 o

spirit_young的博客 374

LeetCode138: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. 解题思路: 拷贝链表时,新节点的random指...

diehuojiang5959的博客 162

LeetCode: Copy List with Random Pointer

思路:O(n^2)参考网上一个O(n)的解法

知行文 728

Leetcode: 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. /** * Definition for singly

ZkvIA的博客 3051

Leetcode:Copy List with Random Pointer

URL:https://leetcode.com/problems/copy-list-with-random-pointer/description/描述:A linked list is given such that each node contains an additional random pointer which could point to any node in the list

zhumingyuan111的博客 276

leetcode:Copy List with Random Pointer

/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL),

yxsh01的专栏 448

LEETCODE: 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. /** * Definition for sing

博客已退休 368

leetcode 138: Copy List with Random Pointer

Same idea with the Clone Graph. Here I simplify the unordered map to connect two pointer addresses. /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * i

mikefan1991的博客 339

LeetCode: Copy List with Random Pointer [138]

【题目】 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. 【题意】 给定一个链表,每个节点除了next指针外,还有一个random指针,指向任意的节点。 要求,复制这样的一个链表 【思路】 思路

Harry Huang 1025

Leetcode: 138. Copy List with Random Pointer

Question: 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. Solution1: public class So...

兔子窝 182

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. Example 1: C++ RandomListNode *co...

qq_34314736的博客 149
上一篇: LeetCode || Sum Root to Leaf Numbers
下一篇: LeetCode: Clone Graph
xuxumoi
博客等级 码龄16年 16粉丝 151原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值