快慢指针用来判断循环链表  记住

快慢指针有四种常用的应用场景:

1.找到有序链表的中点,快指针到头的时候,慢指针就是中点。

2.判断是不是循环链表,快慢指针相遇就是

3.找到循环链表的起点,以链表头结点开始的结点和以快慢指针相遇结点为开始的结点,这两个结点相遇的地方就是开始

4.判断两个链表是不是相交。将其中一个链表收尾相接,然后判断另外一个链表是不是循环链表。

有个博客写的很好:

http://blog.csdn.net/willduan1/article/details/50938210

看见循环链表就向快慢指针,就向看见BST就想中序遍历一样

public boolean hasCycle(ListNode head) {
/*
看的答案,快慢指针,如果一个链表是循环链表,那么快慢指针一定会相遇
因为快指针肯定会套慢指针一圈,就跟跑步套圈一样
*/
if (head==null) return false;
ListNode slow = head;
ListNode fast = head;
while (fast!=null&&fast.next!=null)
{
slow = slow.next;
fast = fast.next.next;
if (fast==slow) return true;
}
return false;
}

第二题是找到循环链表的开始

public ListNode detectCycle(ListNode head){
/*
快慢指针的一个应用,找到链表的循环开始的节点
方法是:一个节点的从链表开头开始,一个节点从快慢节点的相遇点开始,一起走,每次一步,相遇的地方是就是循环的开始
*/
if (head==null||head.next==null) return null;
//快慢指针找到相遇的地方
ListNode slow = head;
ListNode fast = head;
while (fast!=null&&fast.next!=null)
{
slow = slow.next;
fast = fast.next.next;
if (fast==slow) break;
}
//如果fast或者fast的next是null,说明没有环
if (fast==null||fast.next==null) return null;
slow = head;
while (fast!=slow)
{
fast = fast.next;
slow = slow.next;
}
return slow;
}

[LeetCode]141. Linked List Cycle判断循环链表的更多相关文章

  1. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  2. [leetcode]141. Linked List Cycle判断链表是否有环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  3. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  4. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  5. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

  6. [LeetCode] 141. Linked List Cycle 链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  7. 141. Linked List Cycle(判断链表是否有环)

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

  8. [LeetCode] 141. Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  9. LeetCode 141. Linked List Cycle (链表循环)

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

随机推荐

  1. 使用@RequestBody注解获取Ajax提交的json数据

    最近在学习有关springMVC的知识,今天学习如何使用@RequestBody注解来获取Ajax提交的json数据内容. Ajax部分代码如下: 1 $(function(){ 2 $(" ...

  2. WEB组态可视化软件(B/S)

    WEB组态可视化软件 目录 WEB组态可视化软件 1 B/S组态软件 2 组态概念 3 组态产品呈现 4 功能特点 5 组态软件应用场景 6 经典的案例 6.1 某通信基站的组态可视化 7 具体实现 ...

  3. 第7.19节 Python中的抽象类详解:abstractmethod、abc与真实子类

    第7.19节 Python中的抽象类详解:abstractmethod.abc与真实子类 一.    引言 前面相关的章节已经介绍过,Python中定义某种类型是以实现了该类型对应的协议为标准的,而不 ...

  4. PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件移除选项卡的removeTab和clear方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 要从一个QTabWidget中去除选项卡,可用使用removeTab和clear方法. 1.移除选项 ...

  5. "迎圣诞,拿大奖"赛题——SQLI

    0x01 本题所需知识清单: 1.php sprintf()函数漏洞:https://blog.csdn.net/WQ_BCJ/article/details/85057447 2.布尔盲注基本pla ...

  6. (数据科学学习手札99)掌握pandas中的时序数据分组运算

    本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 我们在使用pandas分析处理时间序列数据 ...

  7. 理解js浅拷贝和深拷贝

    理解深拷贝和浅拷贝之前先了解下js中的基本类型和引用类型 1.基本类型: 在js中,数据的基本类型undefined,null,string,number,boolean,在变量中赋的实际值,基本类型 ...

  8. jQuery插件的2种类型

    1.封装方法插件  封装方法插件在本质上来说,是一个对象级别的插件,这类插件首先通过jQuery选择器获取对象,并为对象添加方法,然后,将方法进行打包,封闭成一个插件,这种类型的插件编写简单,极易调用 ...

  9. CF1406E 【Deleting Numbers】

    蒟蒻语 蒟蒻这次 \(CF\) 又双叒叕掉分了,\(C\) 都没有调出来. 还好再最后 \(10\) 秒钟调了下 \(E\) 块长 (块长 \(100\) => \(98\)),才没有掉得那么惨 ...

  10. Codeforces Edu Round 55 A-E

    A. Vasya and Book 简单的取余运用. #include <iostream> #include <cstdio> #include <limits.h&g ...