当前位置:网站首页>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.
边栏推荐
猜你喜欢

【Azure云】服务端点和私有链接有什么区别?观点(1)

1-IMU参数解析以及选择

谷歌数据中心发生“电力事故”造成 3 人受伤

开发模式对测试的影响
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板

Taro小程序跨端开发入门实战

Weilai-software development engineer side record

bus event bus use

2022年裁员潮,失业程序员何去何从?

Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security
随机推荐
HCIP ---- VLAN
blocking non-blocking poll mechanism asynchronous
Gartner reiterates the important value of 'data weaving'
leetcode:334. 递增的三元子序列
从脚本到剪辑,影像大师亲授的后期制作秘籍
JWT implements login authentication + Token automatic renewal scheme
从产品角度看 L2 应用:为什么说这是一个游乐场?
POJ 3101 Astronomy (Mathematics)
chart.js horizontal column chart plugin
Dry goods!ASSANet: Making PointNet++ faster and stronger
阻塞 非阻塞 poll机制 异步
Some tips for using Unsafe
网络安全笔记5——数字签名
EasyCVR级联时,修改下级平台名称将不同步至上级平台
POJ 3101 Astronomy (数学)
4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
TCP/IP笔记
一文带你搞懂中断按键驱动程序之poll机制
自动化测试及Selenium
Network Security Note 6 - Digital Certificates and Public Key Infrastructure