# 有环链表

## 样例

```A:   1->5->10->11->18
↑       ↓
↑       ↓
↑ ← ← ← ←

A:   1->5->10->11->18->null```

## 代码实现

```/**
* Definition for ListNode.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int val) {
*         this.val = val;
*         this.next = null;
*     }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @return: True if it has a cycle, or false
*/
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}

ListNode slow = head, fast = head;

while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}
}```

133 篇文章42 人订阅

0 条评论

## 相关文章

11220

32330

### 每天一算：Remove Linked List Elements

LeetCode上第203号问题：Remove Linked List Elements

9930

### 链表划分

LeetCode 86.Partition List 已知链表头节点指针head与数值X,将所有小于x的节点放在大于或等于x的节点前，且保持这些节点的原来的相...

8340

30440

### LWC 58：725. Split Linked List in Parts

LWC 58：725. Split Linked List in Parts 传送门：725. Split Linked List in Parts Probl...

22280

### 两个排序链表合并

LeetCode 21. Merge Two Sorted Lists 已知两个已排序链表头节点指针L1，L2，将这两个链表合并，合并后仍为有序的，返回合并后...

11330

15630

20160

10430