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: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
141题的延伸,求出循环点。
可以用数学方法证明出slow与find相遇的位置一定是所求的点。
/**
* 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 )
return null;
ListNode fast = head;
ListNode slow = head; while( fast != null && fast.next != null ){
slow = slow.next;
fast = fast.next.next;
if( fast == slow ){
ListNode find = head;
while( find != slow ){
find = find.next;
slow = slow.next;
}
return find;
}
} return null; }
}
leetcode 142. Linked List Cycle II ----- java的更多相关文章
- Java for 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 链表中的环 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 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- (链表 双指针) 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 ...
- 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 ...
- 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 ...
- leetcode 142. Linked List Cycle II 环形链表 II
一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点 head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
随机推荐
- websocket++编译过程
websocket++ 是一个开源 websocket 库,使用websocket++ 能够开发基于websocket 服务. 前一段时间成功编译 websocket++ ,分享一下,编译websoc ...
- 经典线程同步 信号量Semaphore
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- PL/SQL
function & procedure packages function --> arguments or parameters with arguments, IN, read o ...
- Java 多线程间的通讯
在前一小节,介绍了在多线程编程中使用同步机制的重要性,并学会了如何实现同步的方法来正确地访问共享资源.这些线程之间的关系是平等的,彼此之间并不存在任何依赖,它们各自竞争CPU资源,互不相让,并且还无条 ...
- 2013年9月份第1周51Aspx源码发布详情
大型B2B家具门户网源码 2013-9-6 [VS2008]功能描述: 1.门户信息管理 安全取数据即使数据库连接中断不会报错 2.稳定性 每句代码经过3次以上检查.此网站还在运营3年了,没有出过问 ...
- String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。
// 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...
- cometd的服务器配置
CometDServlet必须在web.xml中进行配置,如下: <servlet> <servlet-name>cometd</servlet-name& ...
- 2016 - 1 - 3 国旗选择demo
// // ViewController.m // 国旗 // // Created by Mac on 16/1/3. // Copyright © 2016年 Mac. All rights re ...
- ERP系统上传文档信息下载(十八)
下载的公用方法: /// <summary> /// 下载文档 /// </summary> /// <param name="TableName"& ...
- NOIP2010 关押罪犯 (并查集)
若x,y有关系 将x与y的补集, y与x的补集建立关系 ; maxm=; ..maxm,..] of longint; f:..maxn*] of longint; i,j,m,n,x,y,z:lon ...