Leetcode 142题 环形链表 II(Linked List Cycle II) Java语言求解

题目描述:
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
说明:不允许修改给定的链表。

分析

给出示意图:

对符号的一些说明:

公式演算:
很容易得到:
m=x+y(环的长度两种表示形式);
快指针走两步,慢指针走一步。所以快指针的速度是慢指针的速度的二倍,所以相同时间内,快指针走的长度也是慢指针走的长度的二倍:
有: f=2s;
在快指针走过 圈后两指针相遇,有: m+kn+y=2(m+y);
去括号后有: m+kn=2m+y;
解得: m=kn-y;
又因为:n=x+y;
所以有:m=kn-(n-x);
所以:m=x+n(k-1)。
m是头节点到环起点的长度;
x是相遇点到头节点的长度;
x-m是(k-1)个环的长度。
通过公式的演算,我们能够明白:
找到相遇点后,链表头节点与相遇点节点同时出发,相遇处便是环的起点。相遇点节点多走了(k-1)个环。

/**
 * 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)
            return null;
        ListNode slow = head;
        ListNode fast = head;

        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                break;
            }
        }
        if(fast == null || fast.next == null)
            return null;
        fast = head;
        while(slow != fast){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}

对代码的解释:
1、如果head是空,则不存在环,直接返回null;
2、设置快慢指针开始均指向head,设置循环条件,快指针不为空且快指针的next域指向的不为空进入循环,其中空指针的next域的指向不为空保证快指针可以走两步,否则有可能出现空指针异常;
3、如果快慢指针指向相同节点,则break掉,是相遇点;
4、第一次循环过后,如果fast为空或者fast.next为空,则不存在环,返回null;
5、如果存在环,让fast指针从开始移动,slow指针从相遇点移动,相遇则为起点,将其return即可。

欢迎关注

扫下方二维码即可关注:

原文地址:https://www.cnblogs.com/nicaicai/p/12231850.html

时间: 2024-08-25 20:32:36

Leetcode 142题 环形链表 II(Linked List Cycle II) Java语言求解的相关文章

Leetcode 141题 环形链表(Linked List Cycle) Java语言求解

题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Map集合解法 思路: 创建一个map集合,key为节点,value为地址值,因为ListNode没有重写toString()方法,所以用toString()方法返回的内容作为value. 如果map中存在当前节点的toString()方法返回的内容,则存在环. /** * Definition for

LeetCode 142:环形链表 II Linked List Cycle II

给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 说明:不允许修改给定的链表. Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in th

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode 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-indexe

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? 判断一个链表是否有环. 题解: 设置两个指针p1和p2: p1每次走一步,p2每次走两步,如果在这个过程中,p2为空,则没有环:否则两个指针必然相遇,则有环: 接下来找环的起点,将p1挪动到链表起始,

【leetcode刷题笔记】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

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. Follow up: Can you solve it without using extra space? 解题思路,本题和上题十分类似,但是需要观察出一个规律,参考LeetCode:Linked List Cycle II JAVA实现如下: public ListNode detectCycle(Li

刷题142. Linked List Cycle II

一.题目说明 题目142. Linked List Cycle II,判断一个链表是否有环,如果有返回环的第一个元素,否则返回NULL. 这个题目是141. Linked List Cycle的升级版本,难度是Medium! 二.我的解答 最直观的解答就是用一个unordered_map<ListNode*,int> dp来统计节点出现的次数,如果出现2,则这个就是第一个节点. class Solution{ public: ListNode* detectCycle(ListNode* he

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