【链表】LeetCode 142. 环形链表 II

题目链接

142. 环形链表 II

思路

image

image

image

image

image

image

image

image

image

image

image

image

代码

class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null){
            return null;
        }

        ListNode slow = head;
        ListNode fast = head;

        do{
            slow = slow.next;
            fast = fast.next;
            if(fast != null){
                fast = fast.next;
            }
        }while(slow != null && fast != null && slow != fast);

        if(fast == null){
            return fast;
        }

        fast = head;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }

        return fast;
    }
}
posted @ 2023-01-03 11:43  Frodo1124  阅读(33)  评论(0编辑  收藏  举报