Level:

  Medium

题目描述:

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.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

Follow up:

Can you solve it without using extra space?

思路分析:

  如果存在环,设置一个快指针,一个慢指针,那么快指针一定会追上慢指针相遇,此时相遇的节点一定在环内,这时可以求出环内节点的数目,然后设置一个前指针和后指针初始值都为head,让前指针先走n次,然后前后指针一起走,如果相等时,则该节点就为环入口节点

代码:

public class Solution{
public ListNode detectCycle(ListNode head){
ListNode meetNode=meetNoding(head);
if(meetNode==null)
return null;
ListNode pNode=meetNode;
int count=1;
while(pNode.next!=meetNode){
count++;
pNode=pNode.next;
}
ListNode slow=head;
ListNode fast=head;
for(int i=0;i<count;i++){
slow=slow.next;
}
while(fast!=slow){
fast=fast.next;
slow=slow.next;
}
return fast;
}
//求相遇的节点
public ListNode meetNoding(ListNode head){
if(head==null)
return null;
if(head.next==null)
return null;
ListNode slow=head.next;
ListNode fast=slow.next;
while(slow!=null&&fast!=null){
if(slow==fast)
return fast;
slow=slow.next;
fast=fast.next;
if(fast!=null&&fast.next!=null)
fast=fast.next; //快指针一次走两步
}
return null; //未能相遇则不存在环
}
}

44.Linked List Cycle II(环的入口节点)的更多相关文章

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

  2. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

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

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

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

  5. LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

    1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...

  6. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

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

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

  8. 【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 ...

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

随机推荐

  1. JS中的Number数据类型详解

    Number数据类型 Number类型使用IEEE754格式来表示整数和浮点值,这也是0.2 + 0.3不等于0.5的原因, 最基本的数值类型字面量格式是十进制整数 var a = 10; 1. 浮点 ...

  2. constructor、prototype、isPrototypeOf、instanceof、in 、hasOwnProperty

    constructor.prototype.isPrototypeOf.instanceof.in .hasOwnProperty等等 constructor:对象构造器.存在于原型对象中?,相当于p ...

  3. React(5) --绑定函数事件

    绑定函数事件 在以类继承的方式定义的组件中,为了能方便地调用当前组件的其他成员方法或属性(如:this.state),通常需要将事件处理函数运行时的 this 指向当前组件实例. run(){     ...

  4. OtterCTF - Reverse - Msg Me This

    原文地址:Msg Me This 题目 Category: Reverse Engineering Points: 500 Solves: 15 Description: Rick created a ...

  5. JS基础入门篇(四十三)—ES6(二)

    1.对象简洁表示法 原来写法 var name = "lzf"; var gender = "male"; var fn = function(){consol ...

  6. ConcurrentSkipListMap--跳表的简单使用

    import java.util.Map; import java.util.concurrent.ConcurrentSkipListMap; /** * 跳表的使用 */ public class ...

  7. dubbo所用到的技术

    服务注册:zookeeper 协议:dubbo  Hessian  Rmi 网络编程:netty 动态代理:jdk和Javassist 序列化:Hessian  Dubbo  Json  Java S ...

  8. 使用yum命令报错

    树莓派(Raspberry Pi 3) centos7使用yum命令报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, ...

  9. mui滚动区域的实现

    mui框架实现页面中间区域滚动,头部和底部固定不动,要滚动的区域一定要有mui-scroll-wrapper 和 mui-scroll 包裹 <div class="mui-conte ...

  10. POJ 3348 Cows (凸包模板+凸包面积)

    Description Your friend to the south is interested in building fences and turning plowshares into sw ...