Leetcode 142. Linked List Cycle IIJAVA语言
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语言相关推荐
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- 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 ...
- 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 ...
- [LeetCode] 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 题意:找 ...
- Leetcode - 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 分析: ...
- 【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 ...
- 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 ...
- 【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? ...
最新文章
- cleanmymac3.9.6下载_单耳兔o2oapp下载-单耳兔o2o商城官方版下载v10.6 安卓版
- opencl高斯源码整理
- AGS Server 10.1 切图工具
- mysql按字段同步_MySQL同步(一) 基础知识
- 解读mysql日志_全方位解读 MySQL 日志实现内幕(四)
- ubuntu安装teamviewer教程 linux安装teamviewer教程
- 编程高手必学的内存知识01:深入理解虚拟内存
- 【问题记录】python 函数 传入一个对象返回一个对象值得注意
- python读取配置文件使用_如何使用Python3读取配置文件
- 简述RedHat 7 Linux系统
- 统计学习基础(概念,基本思想,先验知识)
- 3D数字孪生大屏怎么做?你需要了解这款数据可视化软件
- aliplayer 直播在微信和支付宝浏览器自动播放兼容
- 高射炮打蚊子丨在VS 2017里用C语言写经典的冒泡排序
- 物联网微生态系统-鱼缸
- java modelbus_modelbus tcp java
- 传递给Appium服务器以开启相应安卓Automation会话的Capabilities的几点说明
- oracle常见单词_oracle认证考试中,常用单词汇总
- 假想的憧憬,浮云遮住了双眼,
- SpringDataJpa (二)-动态查询多表操作
热门文章
- 【MySQL】二进制分发安装
- phpmyadmin设置编码和字符集gbk或utf8_导入中文乱码解决方法
- Android 解决通过自定义设置打开热点后手机搜索不到热点的问题。
- AngularJS app.run()文档?
- 如何为iPhone 5屏幕分辨率开发或迁移应用程序?
- html5自定义组件样式,Taro 自定义组件样式不生效及解决方案
- java 钩子 64位 操作系统_Java与系统钩子
- win11升级到一半撤销怎么办 windows11升级到一半撤销的解决方法
- java mysql 主键生成策略_主键生成策略
- python从入门到实践课后题_Python 从入门到实践 函数篇 8-6-8习题