环形链表 (Linked List Cycle)

 

思路:

// @Title: 环形链表 (Linked List Cycle)
// @Author: qisiii
// @Date: 2022-02-20 19:22:45
// @Runtime: 0 ms
// @Memory: 42.3 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
         if (head == null || head.next == null) {
            return false;
        }
      ListNode slow=head,fast=head.next;
        while(slow!=fast){
            if(fast==null||fast.next==null){
return false;
            }
            slow=slow.next;
            fast=fast.next.next;
        } 
        return true; 
    }
}

思路:

// @Title: 环形链表 (Linked List Cycle)
// @Author: qisiii
// @Date: 2024-01-04 10:21:01
// @Runtime: 0 ms
// @Memory: 43.1 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
       ListNode slow=head,fast=head;
        while(slow!=null&&fast!=null){
            slow=slow.next;
            fast=fast.next;
            if(fast!=null){
                fast=fast.next;
            }else{
                return false;
            }
            if(slow==fast){
                return true;
            }
        }
        return false;
    }
}

思路:

// @Title: 环形链表 (Linked List Cycle)
// @Author: qisiii
// @Date: 2024-09-04 16:31:03
// @Runtime: 0 ms
// @Memory: 43.3 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) {
 * val = x;
 * next = null;
 * }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while (slow != null && fast != null) {
            slow = slow.next;
            fast = fast.next;
            if (fast != null) {
                fast = fast.next;
                if (slow == fast) {
                    return true;
                }
            }
        }
        return false;
    }
}

思路:

// @Title: 环形链表 (Linked List Cycle)
// @Author: qisiii
// @Date: 2022-02-20 20:43:20
// @Runtime: 0 ms
// @Memory: 42.5 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
         if (head == null || head.next == null) {
            return false;
        }
      ListNode slow=head,fast=head;
        while(fast!=null){
            if(fast==null||fast.next==null){
return false;
            }
            slow=slow.next;
            fast=fast.next.next;
            if(slow==fast){
                return true;
            }
        } 
        return false; 
    }
}

+++ title = “环形链表 (Linked List Cycle)” draft = false +++

思路:

// @Title: 环形链表 (Linked List Cycle)
// @Author: qisiii
// @Date: 2024-01-04 10:06:24
// @Runtime: 4 ms
// @Memory: 43.7 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        HashSet<ListNode> set=new HashSet();
        while(head!=null){
            if(set.contains(head)){
                return true;
            }
            set.add(head);
            head=head.next;
        }
        return false;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18