44.Linked List Cycle II(环的入口节点)

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; //未能相遇则不存在环
    }
}

原文地址:https://www.cnblogs.com/yjxyy/p/11080371.html

时间: 2024-10-15 11:41:34

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

判断链表是否有环及环入口点的求法(Linked List Cycle II )

分为两步 第一步 还是利用快慢指针,如果有环的话在利用快慢指针终会相会于一个节点. 第二步.然后从这节点出发每次出发走一步,同时从根节点出发每次出发也走一步则他们两个指针相遇的地方就是环的入口. 第一步好解释那么第二步是为什么呢? 网上有很多解法大都是从数学的角度来分析,有公式也有推算很不直观,我从图形的角度来看的话就相对理解起来简单很多. 将图摊开成一条线,假设我们有环而且假设快指针就多走了一圈就与慢指针相遇了(多走n圈其实也是一样的,画出图来也不难理解,只是画起来麻烦索性就以一圈来代表) 红

LeetCode之“链表”:Linked List Cycle &amp;&amp; Linked List Cycle II

1. Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 刚看到这道题,很容易写出下边的程序: 1 bool hasCycle(ListNode *head) { 2 ListNode *a = head, *b = head; 3 while(a) 4 { 5 b =

LeetCode: Linked List Cycle ii 解题报告

Linked List Cycle ii 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? SOLUTION 1: 最开始的想法和 Linked List Cycle这一题一样. SOLUTION 2: 同样是两个指针,一快一慢,相遇时跳出循环,只

LeetCode: Linked List Cycle II [142]

[题目] 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? [题意] 给定一个单向链表,如果链表有环,则返回环开始的位置. [思路] 仍然是维护两个指针, p1, p2, p1每次走一步, p2每次走两步 假设进入环之前要走X步,环长为y步,p2第

【Leetcode】Linked List Cycle II

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可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

Leetcode 链表 Linked List Cycle II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle II Total Accepted: 20444 Total Submissions: 66195My Submissions Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? (Easy) 分析: 采用快慢指针,一个走两步,一个走一步,快得能追上慢的说明有环,走到nullptr还没有相遇说明没有环. 代码: 1 /** 2 * Definition for singly-linked list.

leetcode之Linked List Cycle以及Linked List Cycle II

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 摘自剑指offer:当用一个指针遍历链表不能解决问题的时候,可以尝试用两个指针来遍历链表,可以让一个指针遍历的速度快一些,或者让他先在链表上走若干步. 相关的题目有: 求链表的中间节点, 判断一个单向链表是否形成了环形结构 判断有环链表的环的入口节点 一次遍历求链表的倒数第K个