/**
* 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 || head.next == null) {
return false;
}
ListNode sn = head, fn = head.next;
while (fn != null && fn.next != null) {
if (fn == sn) {
return true;
}
sn = sn.next;
fn = fn.next.next;
}
return false;
}
}
Linked List Cycle
最新推荐文章于 2025-05-07 22:36:04 发布