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

自然语言处理:稀疏向量表示 稀疏向量表示是指运用特定的方法和技术,将数据(如文本、图像、信号等)转化为向量形式,且该向量具有大部分元素值为零,仅少数元素值非零的特征,以此来实现对数据的高效存储、处理和分析。 阅读详情

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

基于USB总线的虚拟摄像头(UVC)原理简介 前面文章中我们分析了虚拟声卡的实现,在文章中我们展示了创建一个虚拟麦克风和虚拟扬声器的技术原理。完成了音频的虚拟化之后,这里就要谈论一下对于视频的虚拟化了——虚拟摄像头的实现。关于虚拟麦克风和虚拟扬声器的实现,可以参考。虚拟摄像头应用也是非常普遍的,例如云桌面远程摄像头的实现,直播技术。目前对于虚拟摄像头有三种实现方式:DirectShow虚拟摄像头,这个是一种比较古老的技术;在新版本应用中,该项技术基本失效了,尤其是UWP框架程序,DirectShow虚拟摄像头就失效了。 阅读详情

相关推荐

Android SDK 国内镜像及配置方法(2025最新,包好使!)

android sdk 国内镜像配置方法,纯小白避坑指南,2025最新,亲测包好使!还有包奉上!

土豆是狗的博客 4685

(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

【Audio】基于STM32 I2S移植WM8978 Audio Codec驱动

1. WM8978简介 WM8978是一款低功耗,高质量的立体声编解码器,专为便携式应用,如数码相机或数码摄像机等。 该芯片集成了立体声差分麦克风的前置放大器,并包括扬声器、耳机和差分或立体声线输出的驱动器。外部组件要求减少,因为不需要单独的麦克风或耳机放大器。 WM8978的功能框图如下所示: 2. WM8978硬件连接 使用STM32F429+WM8978硬件平台,通过I2S接口来读写音频数据,I2C接口发送写命令控制WM8978相关功能。 STM32F429与WM8978的引脚连接如下:

ZHONGCAI0901的博客 1万+

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

解决windows10下无法安装.net framework 3.5(包括.net2.0和3.0)

入域电脑,win10系统中安装 .net framework 3.5(包括.net2.0和3.0)

God_me_1的博客 4万+

FFmpeg魔改指南:让RTMP协议完美兼容H265的三种方法(含SRS适配技巧)

本文详细介绍了三种让RTMP协议兼容H265编码的FFmpeg魔改方案,包括基于官方补丁的标准改造、Intel的SVT-HEVC集成方案以及金山云的自定义扩展方案。文章重点解析了各方案的实现原理与适配技巧,并提供了在SRS 6.0服务器上进行深度配置和性能调优的完整实战指南,帮助开发者解决H265推流至传统RTMP服务器的兼容性问题。

weixin_29290947的博客 197

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

一文吃透 SAP IDoc 状态码:从 TEDS1 到 WE02 排障,再到 ABAP 自动化监控

在 SAP 集成场景里,IDoc 往往是最常见、也最容易被低估的一条生命线:采购订单、发票、主数据、库存移动……只要系统之间要稳定地交换结构化业务数据,IDoc 就经常登场。真正让人头疼的,并不是 IDoc 本身,而是它在链路里到底走到了哪一步、卡在了哪里、该找谁背锅。这篇文章把常用的 IDoc 状态码按出站、入站拆开讲清楚,并且把每个状态对应的真实世界故障现象、排查入口、以及可以落地的 ABAP 监控代码串起来,让你看到状态码就能想到动作。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 412

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

8、干式变压器

干式变压器的发展,是人类不断挑战材料科学和热物理极限的过程,每一次技术突破,都是在。温度升高,ε_r反而下降(因为分子活动性增强,极化响应变慢)。:固体导热的热阻比液体对流大一个数量级,这是无法逾越的物理规律。Nomex在高温下不是逐渐分解,而是保持性能直到一个临界点。如果 T_a = 40°C,则 T_c = 146.7°C。干式变压器(固体介质分隔):热场、电场、流场(空气)形成三维氢键网络,即使在高温下也能保持机械强度。:不是在现有技术上修修补补,而是寻找新的。仍在H级绝缘允许范围内(≤180°C)

renpeng2692873032的博客 230

ResNet改进(20):融合Swin Transformer与ResNet

这种混合架构的设计理念是:在保留CNN强大局部特征提取能力的同时,引入Transformer的全局建模优势。我们实现的模型在ResNet18的基础上嵌入了一个简化版的Swin Transformer Block,旨在探索两种范式结合的可能性。通过在ResNet早期阶段引入Transformer模块,我们既保留了CNN强大的局部特征提取能力,又获得了Transformer的全局建模优势。本文提出的ResNet与Swin Transformer混合架构,探索了CNN与Transformer结合的可能性。

2401_82355416的博客 860

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

3、流程引擎下载安装与部署

camunda有多种使用方式,此处先使用独立平台的方式部署引擎,这种用法适合分布式,微服务化,多语言异构系统的使用方式。

leyoliu的博客 5524

经典算法题目: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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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万+

温度PID控制PLC程序

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

算法原理与实践(链表)

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

weixin_30484247的博客 301

VB摄像头监控

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

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、付费专栏及课程。

余额充值