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 ...
随机推荐
- 如何利用一台pc获取百万利益 《标题党》
这是我在quora上看到的一个问题,我看到的被推荐的答案的第一句话就很nb. 有想法很容易做起来很难(不是这句) I’m going to give you something much more v ...
- 吴恩达机器学习-octave笔记
隐藏前缀提示符:PS1('>>') 不显示打印内容:;结尾 字符串:a=’hi’ 屏幕输出:disp(sprint(‘2 decimals:%0.2f’,a)) 生成集合(矩阵):V=1: ...
- javax/servlet/jsp/jstl/core/Config
javax/servlet/jsp/jstl/core/Config springmvc出现的问题. 尝试了各种jar,问题依旧. DispatcherServlet配置如下. <bean id ...
- java获取泛型类型
import com.alibaba.fastjson.TypeReference; return JSON.parseObject(str, new TypeReference<BaseRes ...
- 排序算法练习--JAVA(:内部排序:插入、选择、冒泡、快速排序)
排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... 内部排序: 插入排序:直接插入排序 选 ...
- C++11如何减少内存拷贝次数
C++11中出现了很多迷人的特性.例如智能指针实现高效的内存管理,std::bind和std::function函数封装器,以及lambda实现的函数对象语法糖,都是使我着迷的地方. 而C++11最大 ...
- Python3根据基础概率随机生成选项
想要实现一个功能:不同事件发生的基础概率不同,根据基础概率来随机生成选项. 比如,北京的秋天有四种状态,并分别对应一个基础概率,然后随机生成某一天的天气情况. weatherlist = ['Sunn ...
- VDMA时序分析
VDMA时序分析
- 【mybatis】之trim
<trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...
- [转]Oracle left join \ right join
select 1 from a,b where a.id=b.id(+) 等同于 a left join b on a.id=b.id select 1 from a,b where a.id(+)= ...