Leetcode 142. Linked List Cycle IIJAVA语言

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.

题意:不破坏原链表的情况下判断有没有环,,,,,,

/**
 * 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 ||head.next.next==null)return null;
        ListNode fast=head.next.next;
        ListNode slow=head.next;
        ///判断有没有环。找到相遇点
        while(fast!=slow){
            if(fast.next!=null && fast.next.next!=null){
                fast=fast.next.next;
                slow=slow.next;
            }else{
                return null;
            }
        }
        fast=head;
        while(fast!=slow){
            fast=fast.next;
            slow=slow.next;
        }
        return fast;
    }
}

PS:依然是快慢指针。先让fast走两步和slow走一步,判断有没有环先,有环的话就会相遇。当他们相遇后,让fast回到头,此时走一步。直到和slow相遇。则此时的相遇点位环入口!!!!!详情见https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4

里面有一段推导

时间: 2024-10-06 15:38:39

Leetcode 142. Linked List Cycle IIJAVA语言的相关文章

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 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指

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

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相遇的位置一定是所求的点. /** * Definitio

【LeetCode】Linked List Cycle

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? 做完Linked List Cycle II在做这题简直就是阉割版.. fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false class Solution { public: bool hasCycle(ListNo

[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? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (

【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 OJ - 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? 解题思路: 设置两个指针slow和fast,从head开始,slow一次一步,fast一次两步,如果fast能再次追上slow则有圈. 设slow走了n步,则fast走了2*n步,设圈长度m

LeetCode OJ - 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? 解题思路: 使用快慢指针,快指针每次走两步,慢指针每次走一步,若快指针能追上慢指针,则表明有圈. 代码如下: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNo

141. Linked List Cycle && 142. Linked List Cycle II

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { pu