当前位置:网站首页>鏈錶之雙指針(快慢指針,先後指針,首尾指針)
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
2022-07-05 22:28:00 【CodeStars碼星人】
141. Linked List Cycle](https://leetcode.cn/problems/linked-list-cycle/)
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
這時典型的鏈錶環形問題,鏈錶環形問題的做法就是快慢指針。
快慢指針不要理解錯了,快慢指針不是前後指針,快慢指針的意思是一個走得快,一個走得慢,比如說一個一次走一步,一個走兩步。
這樣的話,如果出現環形的話,那麼走兩步的(在前面的),肯定會在環內追上走一步的(走在後面),就好想操場跑步,跑得快的始終會再次超過跑得慢的那個。
所以就設置兩個快慢指針,看是否會相遇就可以判斷是否成環了。
public boolean hasCycle(ListNode head) {
if(head==null)return false;
ListNode slowNode=head;
ListNode fastNode=head;
while(fastNode!=null&&fastNode.next!=null){
//其實這個還是得在理解的基礎上才寫得出來的,因為如果是沒環的,那麼fast每次走兩步,有可能走到距離null為兩步,也可能走到距離null為一步,所以就while內得那麼寫
slowNode=slowNode.next;
fastNode=fastNode.next.next;
if(slowNode==fastNode)return true;
}
return false;
}
876. Middle of the Linked List

class Solution {
public ListNode middleNode(ListNode head) {
if(head==null)return null;
ListNode slow=head;
ListNode fast=head;
while(fast.next!=null&&fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
}
if(fast.next==null)return slow;
if(fast.next.next==null)return slow.next;
return head;
//此題原理就是
利用快慢指針,快指針每次走兩步,慢指針每次走一步,所以快指針走的距離為慢指針的兩倍,
故當快指針遍曆到鏈錶末尾時,慢指針指向記為中間節點(來自力扣題解)
}

一開始就是將這道題和 上面的快慢指針混淆了。
這個題應該叫做先後指針比較准確,讓一個指針先走,到符合一定條件時停下來。然後再兩個指針一起走。
類似的 還有這道題
思路是一模一樣的
另外就是上面循環鏈錶的加强版,不僅要找到鏈錶是否有環,還要找出環的起點。
思路就是先找快慢指針相遇的地方,然後在相遇的地方和鏈頭開始放一個節點,一起走,兩個節點相遇的地方
就是環的起點。具體推導可以參考代碼隨想錄的解法
代碼如下:
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null)return null;
ListNode result=head;
ListNode slow=head;
ListNode fast=head;
int count=0;
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast) break;
}
if(fast.next==null||fast.next.next==null) return null;
while(result!=slow){
result=result.next;
slow=slow.next;
}
return result;
}
}
边栏推荐
- EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
- Calculation method of boundary IOU
- opencv 判断点在多边形内外
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Interview questions for famous enterprises: Coins represent a given value
- Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
- Overview of concurrency control
- Learning of mall permission module
- Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
- 如何创建线程
猜你喜欢

EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?

The simple problem of leetcode is to split a string into several groups of length K

Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology

2022软件测试工程师涨薪攻略,3年如何达到30K

CA certificate trampled pit

How can Bluetooth in notebook computer be used to connect headphones

从 1.5 开始搭建一个微服务框架——日志追踪 traceId

Pl/sql basic case

数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖

数据泄露怎么办?'华生·K'7招消灭安全威胁
随机推荐
[Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
Leetcode simple question: find the nearest point with the same X or Y coordinate
700. Search in a Binary Search Tree. Sol
Navigation day answer applet: preliminary competition of navigation knowledge competition
Oracle hint understanding
分布式解决方案之TCC
Go语言学习教程(十五)
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Leetcode simple question: check whether each row and column contain all integers
Two stage locking protocol for concurrency control
A substring with a length of three and different characters in the leetcode simple question
2022软件测试工程师涨薪攻略,3年如何达到30K
Qtquick3d real time reflection
Thinkphp5.1 cross domain problem solving
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Leetcode simple question ring and rod
Matlab draws a cute fat doll
如何快速体验OneOS
How to develop and introduce applet plug-ins