[LC] 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
To represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Note: Do not modify the linked list.
/**
* 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 (slow == fast) {
// get the entry point when cur and slow meet
ListNode cur = head;
while(cur != slow) {
cur = cur.next;
slow = slow.next;
}
return slow;
}
}
return null;
}
}
[LC] 142. Linked List Cycle II的更多相关文章
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 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 (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [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
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
随机推荐
- Vue.js——3.增删改查
vue 写假后台 bootstrap 做的样式 代码 <!DOCTYPE html> <html lang="en"> <head> < ...
- Django2.0——中间件
Django中间件middleware本质是一个类,在请求到返回的中间,类中不同的方法会在指定的时机中被触发.setting.py的变量MIDDLEWARE_CLASSES中的每一个元素都是中间件,且 ...
- Spring使用Rabbitmq (简单使用)
1.pom.xml jar包引用 <dependencies> <dependency> <groupId>org.springframework</grou ...
- 吴裕雄--天生自然 JAVA开发学习:基础语法
package test; public class temp { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(Stri ...
- PAT Advanced A1104 Sum of Number Segments (20) [数学问题]
题目 Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For e ...
- vscode template中设置不换行
{ "workbench.colorTheme": "Dark-Dracula", "workbench.iconTheme": " ...
- Java--包密封
参考:http://blog.csdn.net/zhifeiyu2008/article/details/8829637 http://blog.csdn.net/technerd/article/ ...
- iOS 直接使用16进制颜色
在做iOS开发时,一般我们会吸色,就是产品给的图我们一般会吸色,但是最近吸色时候,老大说有较大的颜色偏差,所以要求我们直接使用UI给出的额16进制颜色,你也可以搜索<RGB颜色值转换成十六进制颜 ...
- 手动安装GCC4.8.5
服务器是 redhat 6,安装xgboost时,提示自带gcc 太老, 需要手动升级. 1). 手动安装 mpc-0.8.2.tar.gz, 用默认参数, 安装完后添加系统变量 export LD_ ...
- maven仓库镜像、私服与jdk版本配置
--配置全局镜像,setting.xml <mirrors> <mirror> <id>alimaven</id> <name>aliyun ...