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

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

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

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

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

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

有个博客写的很好:

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

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

  1. public boolean hasCycle(ListNode head) {
  2. /*
  3. 看的答案,快慢指针,如果一个链表是循环链表,那么快慢指针一定会相遇
  4. 因为快指针肯定会套慢指针一圈,就跟跑步套圈一样
  5. */
  6. if (head==null) return false;
  7. ListNode slow = head;
  8. ListNode fast = head;
  9. while (fast!=null&&fast.next!=null)
  10. {
  11. slow = slow.next;
  12. fast = fast.next.next;
  13. if (fast==slow) return true;
  14. }
  15. return false;
  16. }

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

  1. public ListNode detectCycle(ListNode head){
  2. /*
  3. 快慢指针的一个应用,找到链表的循环开始的节点
  4. 方法是:一个节点的从链表开头开始,一个节点从快慢节点的相遇点开始,一起走,每次一步,相遇的地方是就是循环的开始
  5. */
  6. if (head==null||head.next==null) return null;
  7. //快慢指针找到相遇的地方
  8. ListNode slow = head;
  9. ListNode fast = head;
  10. while (fast!=null&&fast.next!=null)
  11. {
  12. slow = slow.next;
  13. fast = fast.next.next;
  14. if (fast==slow) break;
  15. }
  16. //如果fast或者fast的next是null,说明没有环
  17. if (fast==null||fast.next==null) return null;
  18. slow = head;
  19. while (fast!=slow)
  20. {
  21. fast = fast.next;
  22. slow = slow.next;
  23. }
  24. return slow;
  25. }

[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. 怎么用fio测试存储性能

    1 /// -rw=read(100%顺序读) -rw=write(100%顺序写) -rw=randread(100%随机读) -rw=randwrite(100%随机写), 2 ///-rw=rw ...

  2. 从0开始带你成为JVM实战高手(百度网盘)

    狸猫技术窝<从0开始带你成为JVM实战高手> 之前写过几篇 JVM 相关的文章,最近复盘的时候,发现狸猫技术窝<从0开始带你成为JVM实战高手>真的不错,然后就在网上找了一下( ...

  3. 【2020.11.30提高组模拟】剪辣椒(chilli)

    剪辣椒(chilli) 题目描述 在花园里劳累了一上午之后,你决定用自己种的干辣椒奖励自己. 你有n个辣椒,这些辣椒用n-1条绳子连接在一起,任意两个辣椒通过用若干个绳子相连,即形成一棵树. 你决定分 ...

  4. 20190626_二次开发BarTender打印机_C#代码_一边读取TID_一边打印_打印机POSTEK

    demo代码如下: private void btnPrint_Click(object sender, EventArgs e) { if (this.btnPrint.Text == " ...

  5. 树莓派搭建seafile服务器备忘

    用户:pi 密码:raspberry 启用root用户https://blog.csdn.net/chenxd1101/article/details/53437925(防止特殊原因pi用户不能登录时 ...

  6. ABP框架使用Mysql数据库,以及基于SQLServer创建Mysql数据库的架构和数据

    ABP默认的数据库是SQLServer,不过ABP框架底层是EF框架,因此也是很容易支持其他类型的数据库的,本篇随笔介绍在ABP框架使用Mysql数据库,以及基于SQLServer创建MySql数据库 ...

  7. Djang项目部署之sqlite版本升级

    项目环境: centos7 django 2.2.10 问题描述: 使用了django 2.2.12版本开发项目,此版本对应的sqlite需要升级为3.8.0以上. 百度了不少解决方案,缺点:过程繁琐 ...

  8. 索引优化之Explain 及慢查询日志

    索引:本质是数据结构,简单理解为:排好序的快速查找数据结构,以索引文件的形式存储在磁盘中.目的:提高数据查询的效率,优化查询性能,就像书的目录一样.优势:提高检索效率,降低IO成本:排好序的表,降低C ...

  9. centos 6.4-linux环境配置,安装hadoop-1.1.2(hadoop伪分布环境配置)

    1 Hadoop环境搭建 hadoop 的6个核心配置文件的作用: core-site.xml:核心配置文件,主要定义了我们文件访问的格式hdfs://. hadoop-env.sh:主要配置我们的j ...

  10. 【Alpha冲刺阶段】Scrum Meeting Daily2

    [Alpha冲刺阶段]Scrum Meeting Daily2 1.会议简述 会议开展时间 2020/5/23 8:30-9:00 PM 会议基本内容摘要 讨论了基础的分工,以及明确了各自模块需要完成 ...