当前位置:网站首页>链表之双指针(快慢指针,先后指针,首尾指针)
链表之双指针(快慢指针,先后指针,首尾指针)
2022-07-05 22:27: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;
}
}
边栏推荐
- A trip to Suzhou during the Dragon Boat Festival holiday
- Hcip day 16
- Blocking of concurrency control
- 记录几个常见问题(202207)
- 实战:fabric 用户证书吊销操作流程
- 1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
- ESP32 hosted
- The statistics of leetcode simple question is the public string that has appeared once
- Overview of concurrency control
- Storage optimization of performance tuning methodology
猜你喜欢

Stored procedures and stored functions

Distributed resource management and task scheduling framework yarn

Metaverse Ape上线倒计时,推荐活动火爆进行

Search: Future Vision (moving sword)

Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题

Kubernetes Administrator certification (CKA) exam notes (IV)

Win11运行cmd提示“请求的操作需要提升”的解决方法

Concurrency control of performance tuning methodology

A trip to Suzhou during the Dragon Boat Festival holiday

Meituan dynamic thread pool practice ideas, open source
随机推荐
QT creator 7-cmake update
Some tutorials install the database on ubantu so as not to occupy computer memory?
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
509. Fibonacci Number. Sol
Promql demo service
點到直線的距離直線的交點及夾角
a-tree 树的全部展开和收起
Granularity of blocking of concurrency control
Depth first DFS and breadth first BFS -- traversing adjacency tables
thinkphp5.1跨域问题解决
How to develop and introduce applet plug-ins
What about data leakage? " Watson k'7 moves to eliminate security threats
Oracle triggers
A substring with a length of three and different characters in the leetcode simple question
Postman core function analysis - parameterization and test report
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)