题解 | #判断链表中是否有环#
判断链表中是否有环
https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
public boolean hasCycle(ListNode head) {
// 判断链表是否为空
if (head == null) {
return false;
}
// 定义快慢双指针
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next!=null){
// 慢指针移动
slow = slow.next;
// 快指针移动两步
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}
