diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" index ff2aebea5758f48b5a333a5cf3473433bff7c565..522f51c2ac45b1721729b0c279162ee1ea83ebb1 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" @@ -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; + } +} +``` +