LeetCode 142. Lista enlazada Ciclo II 【Java】

Descripción del título

Lista circular vinculada

Código AC

/**
 * 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) {
        ListNode slow=head;
        ListNode fast=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if(slow==fast){
                slow=head;
                while(slow!=fast){
                    slow=slow.next;
                    fast=fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}
201 artículos originales publicados · Me gusta9 · Visitantes más de 10,000

Supongo que te gusta

Origin blog.csdn.net/weixin_40992982/article/details/105473345
Recomendado
Clasificación