leetcode142
- public class Solution {
- public ListNode detectCycle( ListNode head ) {
- if( head == null || head.next == null ){
- return null;
- }
- // 快指针fp和慢指针sp,
- ListNode fp = head, sp = head;
- while( fp != null && fp.next != null){
- sp = sp.next;
- fp = fp.next.next;
- //此处应该用fp == sp ,而不能用fp.equals(sp) 因为链表为1 2 的时候容易
- //抛出异常
- if( fp == sp ){ //说明有环
- break;
- }
- }
- //System.out.println( fp.val + " "+ sp.val );
- if( fp == null || fp.next == null ){
- return null;
- }
- //说明有环,求环的起始节点
- sp = head;
- while( fp != sp ){
- sp = sp.next;
- fp = fp.next;
- }
- return sp;
- }
- }
补充一个python的实现:
- class Solution:
- def detectCycle(self, head: ListNode) -> ListNode:
- if head == None or head.next == None:
- return None
- slow,fast = head,head
- while fast != None and fast.next != None:
- slow = slow.next
- fast = fast.next.next
- if slow == fast:
- break
- if fast == None or fast.next == None:
- return None
- slow = head
- while slow != fast:
- slow = slow.next
- fast = fast.next
- return slow
leetcode142的更多相关文章
- 【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II
[算法训练营day4]LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表 ...
- [Swift]LeetCode142. 环形链表 II | Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- LeetCode142:Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- Leetcode142 环形链表
很多题解没有讲清楚非环部分的长度与相遇点到环起点那部分环之间为何是相等的这个数学关系.这里我就补充下为何他们是相等的.假设非环部分的长度是x,从环起点到相遇点的长度是y.环的长度是c.现在走的慢的那个 ...
- Leetcode142. Linked List Cycle II环形链表2
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- leetcode142 Linked List Cycle II
""" Given a linked list, return the node where the cycle begins. If there is no cycle ...
- LeetCode142 环形链表 II
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? //章节 - 链表 //二.双指针技巧 //2.环 ...
- LeetCode 142
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
随机推荐
- Verilog强制激励语法
Verilog强制激励语法 1. 在一个过程块中,可以用两种不同的方式对信号变量或表达式进行连续赋值. 过程连续赋值往往是不可以综合的,通常用在测试模块中. 两种方式都有各自配套的命令来停止赋值过程. ...
- Markup解析XML——文档,说明
链接:http://pan.baidu.com/s/1slMwEc9 密码:slz7 上面是网盘的地址,因为来源已经找不到了,在这里给这个作者说声谢谢. 轻量级的XML解析器使用比较简单,下载Mark ...
- Jacobi-Anger expansion
[转载请注明出处]http://www.cnblogs.com/mashiqi 2017/06/16 适合于自己的关于Jacobi-Anger expansion的推导方法,这里记下来,方便以后查阅. ...
- noip-2006普及组-数列- 【模拟-找规律-快速幂】
链接:https://ac.nowcoder.com/acm/contest/153/1047 来源:牛客网 题目描述 给定一个正整数k( ≤ k ≤ ),把所有k的方幂及所有有限个互不相等的k的方幂 ...
- 初识vue小结
初识vue 大家都那么热爱他一定有原因,我也特想了解. 我来咯, 首先用vue开发版,用一个标签在head中插入,script标签src属性引入vue文件,就像jquey一样在script,但是放在h ...
- Opensource Licenses
协议列表https://www.gnu.org/licenses/license-list.htmlhttps://opensource.org/licenses/alphabetical 协议选择参 ...
- vue实现淘宝购物车功能
淘宝购物车功能,效果如下图 非常简单的逻辑,没有做代码的封装,代码如下 <div class="list-container"> <div class=" ...
- Cache架构设计
Cache策略 定时过期策略 定时过期的好处是Cache节点的个数符合实际需求,不会造成资源滥用和服务器压力 定时过期适合访问量较大,实时性要求不高的情况 如果访问量小,定时过期会造成Cache命中率 ...
- Django学习笔记之数据库-QuerySet_API
QuerySet API 我们通常做查询操作的时候,都是通过模型名字.objects的方式进行操作.其实模型名字.objects是一个django.db.models.manager.Manager对 ...
- [C#]时间格式和字符串的相互转换
一.字符串转化为时间格式 string date = "2018/9/27 10:53:56"; DateTime dt1 = DateTime.Parse(date);//方式1 ...