题解 | #判断链表中是否有环#
判断链表中是否有环
https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
import java.util.*; public class Solution { public boolean hasCycle(ListNode head) { // 空链表 if(head == null) return false; // 只有一个节点 if(head.next == null) return false; // 快慢指针 ListNode slow = head; ListNode fast = head.next; // 判断是否有环 while(fast!=null && fast.next!=null){ if(slow == fast){ return true; } slow = slow.next; fast = fast.next.next; } return false; } }