Ch2-5: find the beginning of loop in a circular linked list---two solutions

【华为OD机试真题 新系统】1029、字符串处理 | 机试真题+思路参考+代码解析(C++、Java、Py、C语言、JS) 【华为OD机试真题 新系统】1029、字符串处理 | 机试真题+思路参考+代码解析(C++、Java、Py、C语言、JS) 华为od机试,华为od机试真题,华为od机试E卷,华为odE卷,华为od机试真题E卷,华为OD,华为OD机试,华为OD机试真题,华为od机考,华为od机试D卷,华为od机试真题D卷,华为od手撕代码,华为od,华为od机试C卷,华为od e卷,华为2026e卷,od机试,2026E卷,华为OD机试E卷,什么是华为od,华为od介绍,全面了解华为od,2026华为od,如何备战华为od, 阅读详情

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.

DEFINITION

Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.

EXAMPLE

Input: A -> B -> C -> D -> E -> C [the same C as earlier]

Output: C


这题有2中做法,书上的那个还没仔细看(2个指针,一个+2,一个+1,如果重复了,就说明有环。但是怎么找到环的头节点呢?关键是:

在重复的时候,把slow重新指向head,同时把fast的速度改为一,那么他们再次相遇的时候就是环路的头了)公式请参考hawstein。

I thinks the 2nd idea is simple as similar to early solution: use a hashmap to have key(node addr), if the addr is duplicate, it means there is a loop.

#include 
#include 
using namespace std;

typedef struct node{
    int data;
    node *next;
}node;

// initialize a linkedlist with a loop,
// the n is the total nodes, m is the starting 
// node of the loop, still a singly linked list 
node* init(int *a, int n, int m){  
    node *head, *p, *q;
    for(int i=0; idata = a[i];
        if(i==m) q = nd;
        if(i==0){
            head = p = nd;
            continue;
        }
        p->next = nd;
        p = nd;
    }
    p->next = q;
    return head;
}

// seems like a math contest idea
node* loopstart0(node *head){   // see the explaination
    node *fast = head, *slow=head;
    while(fast || fast->next){
        fast = fast->next->next;
        slow = slow->next;
        if(fast==slow)
            break;
    }
    if(!fast || !fast->next) return NULL;
    slow = head;
    while(fast!=slow){
        fast = fast->next;
        slow = slow->next;
    }
    return fast;
}

// method 2: simple and easy to understand
map hashmap;

node* loopstart1(node *head){   
    while(head){
        if(hashmap[head]) return head;
        else{
            hashmap[head] = true;
            head = head->next;
        }
    }
    return head;
}
int main(){
    int n =6, m =2 ;// mdata<

输出结果:

Executing the program....
$demo 
3

学Simulink--基于电力电子变换器的建模与仿真实例:LLC谐振变换器在高频AC-DC转换中的建模 本文详细介绍了使用Simulink+Simscape Electrical对LLC谐振变换器进行建模与仿真的完整流程。LLC谐振变换器因其高效率、低噪声的特点,广泛应用于高频AC-DC转换场景。文章从背景原理出发,逐步讲解系统结构设计、建模步骤(包括AC源、整流器、LLC变换器、负载等模块搭建)、控制策略实现(固定频率/变频/PID控制)和仿真设置。通过分析仿真结果的关键指标,提出参数整定和算法优化建议。该建模方法为电力电子设计人员提供了实用的仿真工具,有助于优化LLC变换器性能,缩短开发周期。 阅读详情

相关推荐

企业固定资产管理的SAP实战:FICO模块配置与折旧范围设置详解

本文深入解析了SAP FICO模块中固定资产会计配置的核心实践,重点阐述了折旧范围的设计哲学与配置逻辑。通过详解折旧表、折旧范围、资产分类及折旧码等关键配置步骤,指导企业如何搭建满足多会计准则(如CAS、IFRS)与税务要求的资产管理体系,实现资产购置、折旧计提与报废清理的自动化与精准化。

wdx01234567的博客 729

(CodeForces - 600D)Area of Two Circles' Intersection

(CodeForces - 600D)Area of Two Circles’ Intersection The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle. The

The wind of freedom blows 698

PET,CT图像融合笔记

PET-CT怎样进行图像融合?图像融合实现CT与PET图像的融合 OpenCV实现(附实验结果)

chenran187906的博客 8416

Chapter 2 | Linked Lists--查找循环链表中的环的开始结点

2.5  Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION Circular linked list: A (corrupt) linked list in which a node’s nextpointer poin

wenqian 'blog 3916

深入解析反射式DLL注入:从原理到实战优化

本文深入解析反射式DLL注入技术,从原理到实战优化全面剖析。通过PE文件内存重构、反射加载器实现及免杀增强方案,展示如何绕过安全检测并提升性能。适用于红队行动、内网渗透等场景,是高级攻防对抗中的关键技术。

weixin_28339967的博客 406

ABAQUS单元刚度矩阵输出全攻略:从理论到实践(含MTX/DAT文件对比)

本文详细解析了ABAQUS中单元刚度矩阵的输出方法,涵盖理论原理、MTX与DAT文件格式对比及二次开发应用。通过具体操作步骤和性能优化建议,帮助用户高效获取并利用刚度矩阵数据,特别适合需要进行有限元分析验证和二次开发的工程师。

lg888的博客 1008

OpenRisc-6-wishbone实验

引言  学习和研究OR,wishbone是绕不开的问题。本小节就做一个简单的实验,进一步加深对wishbone总线的理解。   6.1 总线timing 请参考官方spec,链接如下: http://opencores.org/opencores,wishbone 附: http://blog.youkuaiyun.com/column/details/ce123-wishbone.html

Rill的专栏 5152

5G核心网NAS消息解析:从协议字段到实际抓包分析(附Wireshark示例)

本文深入解析5G核心网NAS消息的实战分析方法,从协议字段解读到实际Wireshark抓包诊断。通过详解扩展协议鉴别器、安全头类型、流程事务标识等核心字段,并结合注册流程故障排查实例,指导工程师快速定位NAS层问题,提升5G网络调试与协议分析效率。

grass的博客 532

Circles Gym - 101853I

https://cn.vjudge.net/problem/Gym-101853I #include <iostream> #include <cstring> #include <queue> #include <vector> #include <algorithm> #include <cstdio> #includ...

newhonor 314

【YOLOv11】014、YOLOv11模型导出:PyTorch到ONNX格式转换与优化

摘要:本文详细介绍了如何将PyTorch训练的YOLOv11模型高效转换为ONNX格式。重点包括:分离模型特征提取与后处理、设置动态维度、优化ONNX算子、验证转换正确性等关键步骤。作者分享了实际项目中的经验教训,如版本对齐、算子兼容性处理、后处理分离等实用技巧,并推荐使用onnx-simplifier工具优化模型。文章强调模型转换是一个需要反复调试的过程,建议保留转换记录形成知识库,以提高部署效率并避免常见错误。

企鹅码农的专属播客,分享10多年的实战案例和经验解读,包含YOLO、RT-DETR、AI等各个工作领域。 307

【图像超分】论文精读:Enhanced Deep Residual Networks for Single Image Super-Resolution(EDSR和MDSR)

最近对超分辨率的研究随着深度卷积神经网络 (DCNN) 的发展取得了进展。特别是,残差学习技术表现出更好的性能。在本文中,我们开发了一个增强的深度超分辨率网络(EDSR),其性能超过了当前最先进的 SR 方法。我们模型的显着性能改进是由于通过删除传统残差网络中不必要的模块进行优化。当我们稳定训练过程时,通过扩展模型大小可以进一步提高性能。我们还提出了一种新的多尺度深度超分辨率系统(MDSR)和训练方法,该方法可以在单个模型中重建不同缩放因子的高分辨率图像。所提出的方法在基准数据集上比最先进的方法表现出更好的

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 7926

leetcode -- 547. Friend Circles

There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then ...

猪の成长日记 218

大模型多智能体协作面试题及参考答案

在多智能体系统里,代理定义自身目标或效用函数的过程与系统架构、应用场景紧密相关。从本质上讲,目标是代理行为的导向,而效用函数则是对目标达成程度的量化评估。代理通常依据自身角色、系统全局需求以及与其他代理的交互关系来确立目标。比如在合作型 MAS 中,代理目标可能需要与全局目标对齐,像多个物流机器人协作完成货物配送时,每个机器人的目标会围绕 “高效完成分配任务”“避免路径冲突” 等展开,其效用函数可能涉及任务完成时间、能耗等指标的加权求和。目标定义的方式存在多种类型。

大模型大数据攻城狮的专栏 1025

经典算法题目:Cracking the coding interview 问题与解答

Cracking the coding interview--问题与解答 March 14, 2013 作者:Hawstein 出处:http://hawstein.com/posts/ctci-solutions-contents.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注

我的专栏 7584

蓝牙模块HM-10手册

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

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

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

Your task is to Calculate a + b.

一、Your task is to Calculate a + b. Input:The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. Output:For each pair of input integers

天道酬勤 1万+

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

算法原理与实践(链表)

  链表   大纲  1. 链表介绍  2. 基本操作  3. Dummy Node  4. 追赶指针技巧  5. 例题分析   链表介绍  单向链表(singly linked list),每个节点有一个 next 指针指向后一个节点,还有一个成员变量用以储存数值;  双向链表(Doubly Linked List),还有一个 prev 指针指向前一个节点。  Search: O(n), D...

weixin_30484247的博客 301

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

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

POJ 2546 Circular Area [相交园面积]【计算几何】

题目链接:http://poj.org/problem?id=2546Circular Area Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5662 Accepted: 2215 DescriptionYour task is to write a program, which, given two

Tabris的博客 1658

cc150做题计划

计划:12.29.2013 - 1.29.2013 做完第一遍题目,然后2月做leetcode。 2月底面试前完成200题C++。 这里记录一下ctci学习的参考资料。 1. hawstein的blog 2. https://github.com/ChengTian/CtCI 3. absolute C++ 5th 4. Primer C++ 5. learncpp:http

纯黑老白的专栏 2909

ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods

In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes: it means: (from leetcode blog: 点击打开链接) _______3______ / \

纯黑老白的专栏 1330

Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)

这个有点像DSP FPGA Chang Choo的题目,一般是放大插值,DFT,IIR之类的Verilog题目。 题目在这:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees.

纯黑老白的专栏 1295

Ch2-2: return the nth to the last node data of a singly linked list

In this problem, I need to find the nth to the last node data of a singly linked list.  In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack an

纯黑老白的专栏 1290

1/22/2014: 纪念第一个C++ programmer电面--F家

经过20天的CC刷题(其实是学习),才刚刚做到第四章。看看其他非ACM牛人,都是leetcode,CC,EPI滚瓜烂熟才去面。而且onsite确实是靠实力,要有做过大project之后才能真正融会贯通C++精神。 Anyway,谢谢郭志给的refer,不然连电面的机会都没有。 开头是C++基础题: 1. Fib的2种写法:recursive和DP 2. Struct和Class的区别;在C

纯黑老白的专栏 1274

Ch1-7: clear the row i and column j iff a[i][j]==0

in this problem, both monish001 and hawstein use O(n^2) solution which first tranverse the char** a to find the a[i][j]==0 and mark that row/col to be: TRUE. Then the send loop make the TURE row/col

纯黑老白的专栏 1207

复习一下dfs和bfs 用java

dfs 和 bfs都是用来找从source 到 target的path的算法。 dfs用的是recursion,bfs则用queue

纯黑老白的专栏 1191

Ch3-1: use a single array to implement three stacks.

Here it explains why Hawstein use this in his constructor, because he use size a parameter in his constructor while "size" is also a member variable(it's private), so he use this->size = size, explici

纯黑老白的专栏 1097

ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one

// Ch4.7: You have two very large binary trees: T1, with millions of nodes, and T2, // with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 #include #include #includ

纯黑老白的专栏 1030
上一篇: Ch2-4:add reverse 1 digit format number in linked list
下一篇: Ch3-1: use a single array to implement three stacks.
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值