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

Follow up:
Can you solve it without using extra space?

解题思路,本题和上题十分类似,但是需要观察出一个规律,参考LeetCode:Linked List Cycle II

JAVA实现如下:

    public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
boolean isLoop=false;
while (fast != null) {
slow = slow.next;
ListNode temp = fast.next;
if (temp == null)
return null;
fast = temp.next;
if (fast == slow){
isLoop=true;
break;
}
}
while (isLoop) {
if (slow == head)
return slow;
slow = slow.next;
head = head.next;
}
return null;
}

Java for LeetCode 142 Linked List Cycle II的更多相关文章

  1. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

  2. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

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

  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. To r ...

  4. LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java

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

  5. leetcode 142. Linked List Cycle II ----- java

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

  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. Note ...

  7. leetcode 142. Linked List Cycle II 环形链表 II

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  8. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  9. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

随机推荐

  1. [转]window.opener用法

    window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open("subForm.html" ...

  2. 基于spring mvc的注解DEMO完整例子

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mv ...

  3. spring 第一篇(1-1):让java开发变得更简单(下)转

    spring 第一篇(1-1):让java开发变得更简单(下) 这个波主虽然只发了几篇,但是写的很好 上面一篇文章写的很好,其中提及到了Spring的jdbcTemplate,templet方式我之前 ...

  4. Bzoj2705 Longge的问题

    Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Description Longge的数学 ...

  5. Ubuntu学习总结-03 安装软件 & 技巧

    1 UBuntu 安装 Googole Chrome 首先下载软件 wget https://dl.google.com/linux/direct/google-chrome-stable_curre ...

  6. 在tp中使用mongo数据库并建立连接的实例

  7. php升级

    由于5.3版本不支持[]数组简单声明,故升级版本. apt-get install python-software-properties apt-get install -y ppa-purge ad ...

  8. 初学hibernate之缓存

    一.1.Session级别缓存属于一级缓存,持久化对象保存在Session一级缓存中(一级缓存引用持久化对象地址),只要session不关闭,一级缓存就存在,缓存中对象也不会被回收: Session会 ...

  9. java调用本地方法的时候报错 could not find the main class:xx.program will exit

    如图所示,当在java调用本地方法的时候报错 我的解决办法是把dll文件放到System.out.println(System.getProperty("java.library.path& ...

  10. 编译安装apache-2.4.18

    apache安装时, 必须要apr和apr-util, 这两个包是必须的 当下载apache的版本过高, 如: apache-2.4.18, 那么要求的apr或apu=apr-util版本将至少在1. ...