未验证 提交 c956e3cf 编写于 作者: ZhengAu's avatar ZhengAu 提交者: GitHub

添加141.LinkedListCycle的Java解法

上级 0b2efdc7
......@@ -257,3 +257,26 @@ class Solution:
# 退出循环,则链表有结束,不可能为环形
return False
```
[zhengpj95](https://github.com/zhengpj95) 提供 Java 代码
```java
public class Solution {
public boolean hasCycle(ListNode head) {
//链表为空或只有一个结点,无环
if (head == null || head.next == null) return false;
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
// 快慢指针相遇则表示有环
if (fast == slow) return true;
}
//fast指针到末尾,无环
return false;
}
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册