Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.

题意:不破坏原链表的情况下判断有没有环,,,,,,

/*** 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) {/不让修改原来的结构。本来还想用断链法.if(head==null || head.next==null ||head.next.next==null)return null;ListNode fast=head.next.next;ListNode slow=head.next;///判断有没有环。找到相遇点while(fast!=slow){if(fast.next!=null && fast.next.next!=null){fast=fast.next.next;slow=slow.next;}else{return null;}}fast=head;while(fast!=slow){fast=fast.next;slow=slow.next;}return fast;}
}

PS:依然是快慢指针。先让fast走两步和slow走一步,判断有没有环先,有环的话就会相遇。当他们相遇后,让fast回到头,此时走一步。直到和slow相遇。则此时的相遇点位环入口!!!!!详情见https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4

里面有一段推导

转载于:https://blog.51cto.com/fulin0532/1905477

Leetcode 142. Linked List Cycle IIJAVA语言相关推荐

  1. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

  2. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

  3. Leetcode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法

    题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...

  5. [LeetCode] 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 题意:找 ...

  6. Leetcode - 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 分析: ...

  7. 【LeetCode】142 - Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. 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 ex ...

  9. 【leetcode】Linked List Cycle I II

    Given a linked list, determine if it has a cycle in it. Can you solve it without using extra space? ...

最新文章

  1. cleanmymac3.9.6下载_单耳兔o2oapp下载-单耳兔o2o商城官方版下载v10.6 安卓版
  2. opencl高斯源码整理
  3. AGS Server 10.1 切图工具
  4. mysql按字段同步_MySQL同步(一) 基础知识
  5. 解读mysql日志_全方位解读 MySQL 日志实现内幕(四)
  6. ubuntu安装teamviewer教程 linux安装teamviewer教程
  7. 编程高手必学的内存知识01:深入理解虚拟内存
  8. 【问题记录】python 函数 传入一个对象返回一个对象值得注意
  9. python读取配置文件使用_如何使用Python3读取配置文件
  10. 简述RedHat 7 Linux系统
  11. 统计学习基础(概念,基本思想,先验知识)
  12. 3D数字孪生大屏怎么做?你需要了解这款数据可视化软件
  13. aliplayer 直播在微信和支付宝浏览器自动播放兼容
  14. 高射炮打蚊子丨在VS 2017里用C语言写经典的冒泡排序
  15. 物联网微生态系统-鱼缸
  16. java modelbus_modelbus tcp java
  17. 传递给Appium服务器以开启相应安卓Automation会话的Capabilities的几点说明
  18. oracle常见单词_oracle认证考试中,常用单词汇总
  19. 假想的憧憬,浮云遮住了双眼,
  20. SpringDataJpa (二)-动态查询多表操作

热门文章

  1. 【MySQL】二进制分发安装
  2. phpmyadmin设置编码和字符集gbk或utf8_导入中文乱码解决方法
  3. Android 解决通过自定义设置打开热点后手机搜索不到热点的问题。
  4. AngularJS app.run()文档?
  5. 如何为iPhone 5屏幕分辨率开发或迁移应用程序?
  6. html5自定义组件样式,Taro 自定义组件样式不生效及解决方案
  7. java 钩子 64位 操作系统_Java与系统钩子
  8. win11升级到一半撤销怎么办 windows11升级到一半撤销的解决方法
  9. java mysql 主键生成策略_主键生成策略
  10. python从入门到实践课后题_Python 从入门到实践 函数篇 8-6-8习题