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. SQL Network Interfaces, error: 50 - 发生了 Local Database Runtime 错误。无法创建自动实例。

    今天在用VS2013自带的LocalDB调整数据库时出错,在网上也搜到许多方案,如卸载SQLServer LocalDB的程序.重新创建实例等都没有解决我的问题,也重新修改以及修复Vs,问题依旧存在, ...

  2. 表单form action的url写法

    在写web页面时,标签 是很常见的元素,它的一个属性是action,用来标识将表单交给谁去处理.很显然,这里有一个地址的问题,而且是在服务器这边的地址.比如服务器内的一个servlet.   那么这个 ...

  3. RONOJ 6今明的预算方案(有依赖的背包)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  4. php验证是否是中文

    header("content-type:text/html;charset=utf-8");$str = "编";if (preg_match("/ ...

  5. Spring MVC 读取静态资源时404错误

    背景:web.xml配置时拦截策略是拦截所有请求: <servlet> <servlet-name>springmvc</servlet-name> <ser ...

  6. C标准函数库(常用部分)

  7. ewebeditor编辑器ASP/ASPX/PHP/JSP版本漏洞利用总结及解决方法

    这个编辑器按脚本分主要有4个版本,ASP/ASPX/PHP/JSP 每个版本都有可以利用的漏洞.判断网站是否使用了eWebEditor查看程序源代码,看看源码中是否存在类似”ewebeditor.as ...

  8. 关于js函数中的异步编程

    大家都说js 是单线程的应用,但是随着技术的发展,js的发展已经不仅仅局限于单线程了.因为现在很多都是异步了,所谓的异步,就是类似于ajax,写了一个回调函数,当我的服务还在这个地方的时候,等着他去排 ...

  9. 微信稳居Android App排行榜4月份国内榜首

    根据某机构通过对Android样本访问行为的持续监测数据进行样本属性加权并根据iOS/Android用户调研数据建模推总得出中国移动互联网用户规模以及相应的用户结构数据显示,2015年4月份国内And ...

  10. 如何用sql批量删除一个id段内的dedecms文章?

    之前因为ytkah批量添加了dedecms文章,数量有些多,后面出现问题了,想要删除一部分织梦文章,后台一篇篇删,删到手软(相关内容:修改dedecms关键词到手软),于是就想到了sql数据库操作!那 ...