当前位置:网站首页>leetcode 142. Circular linked list II
leetcode 142. Circular linked list II
2022-06-27 16:59:00 【chenyson】
difficulty : secondary
The frequency of :114
subject : Given the head node of a linked list head , Return to the first node of the link where the list begins to enter . If the list has no links , Then return to null.
If there is a node in the linked list , It can be done by continuously tracking next The pointer reaches again , Then there is a ring in the linked list . To represent a ring in a given list , The evaluation system uses an integer pos To indicate where the end of the list is connected to the list ( Index from 0 Start ). If pos yes -1, There are no links in the list . Be careful :pos Not passed as an argument , Just to identify the actual situation of the linked list .

Their thinking : Speed pointer
Be careful :
- law : The length from the node where the fast and slow pointers meet to the starting node of the ring =head The length to the ring start node 【 Use the slow pointer to walk , A from head Start , A place where the fast and slow pointers meet , Finally, the index is 0 The nodes meet 】
- Practice the circular linked list 141 while Don't forget in your judgment fast.next!=null
Code :
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast=head,slow=head;
//fast.next!=null Can't forget If you take two steps, you may encounter the next step and there is no way to go
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow) {
fast=head;
while(fast!=slow){
fast=fast.next;
slow=slow.next;
}
return slow;
}
}
return null;
}
}
边栏推荐
- About how vs2019c # establishes the login interface, the user name and password entered must match the records in the access database
- 字节跳动埋点数据流建设与治理实践
- #yyds干货盘点#简述chromeV8引擎垃圾回收
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- 数据中心表格报表实现定制统计加班请假汇总记录分享
- 06. First introduction to express
- Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration
- Special function calculator
- QT5 之信号与槽机制(信号与槽的基本介绍)
- Four characteristics of transactions
猜你喜欢

如何提升IT电子设备效能管理

d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?

Etcd可视化工具:Kstone部署(一),基于Helm快速部署

Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'

Oracle概念二

Source NAT address translation and server mapping web page configuration of firewall Foundation

Simulated process scheduling

Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance

07. Express routing

What is RPC
随机推荐
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
Autodesk NavisWorks 2022 software installation package download and installation tutorial
LeetCode 124. Binary tree maximum path sum - binary tree series question 8
EMQ helps Qingdao Yanbo build a smart water platform
阿里云刘珅孜:云游戏带来的启发——端上创新
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
Realize simple three-D cube automatic rotation
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
LeetCode每日一练(两数之和)
Halcon: discrete digital OCR recognition
鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
Simulated process scheduling
What do fast fashion brands care more about?
Mobile terminal click penetration
Some details of Huawei OSPF
Domain name binding dynamic IP best practices
Oracle概念二
关于#mysql#的问题:问题遇到的现象和发生背景
特殊函数计算器