/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) return false;
ListNode slow = head;
while (head != null && head.next != null) {
head = head.next.next;
slow = slow.next;
if (head == slow) return true;
}
return false;
}
}
LeetCode 141. Linked List Cycle
最新推荐文章于 2022-09-06 21:21:59 发布