当前位置:网站首页>The brave rice rice, does not fear the brush list of 】 list has a ring
The brave rice rice, does not fear the brush list of 】 list has a ring
2022-08-10 11:09:00 【rice, rice】
Foreword
This series mainly organizes the hand-tear code questions that need to be mastered in the interview.This section describes the problem of having cycles in linked lists.
Article table of contents
First, BM6 determines whether there is a cycle in the linked list
(1) Set the fast and slow pointer, and the fast pointer moves at a timeTwo spaces, the slow pointer moves one space at a time;
(2) If the fast pointer is not empty, then it will keep looping;
(3) If the fast pointer is equal to the slow pointer, it proves that there is a cycle in the linked list, if the fast pointer is equal to the slow pointerIf the pointer is empty, it proves that there is no cycle in the linked list.
function hasCycle( head ) {// write code herevar slow = head;var fast = head;while(fast && fast.next){slow = slow.next;fast = fast.next.next;if(slow == fast) return true}return false}
Second, the entry node of the ring in the BM7 linked list
(1) Set the fast and slow pointer, the fast pointer moves two spaces at a time, and the slow pointer moves one space at a time;
(2) Same as the previous question, if the fast pointer == the slow pointer, it proves that there is a loop, and if the fast pointer is empty, there is no loop;
(3) If the fast pointer is equal to the slow pointer, let the slow pointer start from the beginning,Both hands move forward one frame at a time, and when the fast hand equals the slow hand, that position is the beginning of the ring.
边栏推荐
- 什么是抽象类
- 高阶组件使用
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
- "Data Strategy" Results-Driven Enterprise Data Strategy: Organization and Governance
- HDU 1520 Anniversary party (tree dp)
- 第2章-矩阵及其运算-矩阵运算(2)
- chart.js水平柱状图插件
- Redis设计与实现
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- 4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
猜你喜欢
Weilai-software development engineer side record
短视频软件开发——平台同质化如何破局
2022年裁员潮,失业程序员何去何从?
"MySQL Advanced Chapter" 6. Principles of index creation and design
Get started quickly and conquer three different distributed architecture calling schemes
技能大赛训练题:组策略一
owl.carousel海报卡片Slider轮播切换
从产品角度看 L2 应用:为什么说这是一个游乐场?
组合模式:Swift 实现
FastReport.Net 2022.2.17 Crack
随机推荐
getParameter()与 getAttribute()的用法与区别
程序员追求技术夯实基础学习路线建议
Research on motion capture system for indoor combined positioning technology
Pycharm终端出现PS问题、conda或activate不是内部命令问题..
Redis设计与实现
从脚本到剪辑,影像大师亲授的后期制作秘籍
企业如何判断数据治理是否成功?
Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security
【无标题】
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
what is bsp in rtems
干货!ASSANet:让PointNet++更快更强
网络文化经营许可证
使用cpolar远程连接群晖NAS(升级固定链接2)
Emulate stm32 directly with proteus - the programmer can be completely discarded
突破次元壁垒,让身边的玩偶手办在屏幕上动起来!
让软件飞——“X+”技术揭秘
bus event bus use
「首席工程师」首席(Principal )工程师修炼之道
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况