leetcode01 --- 环形链表判定

题目

. - 力扣(LeetCode) 环形链表判定

代码

复制代码
/**
 * 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) {
        ListNode node = head;
        ListNode preNode = head;
        while (node.next != null) {
            boolean cycle = false;
            while (preNode != node) {
//                System.out.println("val = " + preNode.val);
                if (node.next == preNode) {
                    return true;
                }
                preNode = preNode.next;
            }
//            System.out.println("cycle = " + cycle);
            //if (cycle) {
            //    return true;
            //}
            node = node.next;
            preNode = head;
        }
        return false;
    }
}
相关推荐
专注仿真2 小时前
ACM算法
算法·深度优先
顺顺 尼5 小时前
linux 进程信号(上)
linux·c++·算法
罗西的思考5 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 ---(5)--- GRPO
人工智能·算法·机器学习
richard_yuu5 小时前
动态规划:强化学习的「数学基础」,从 MDP 到值迭代
深度学习·神经网络·算法·yolo·机器学习·动态规划
mmmmath_35 小时前
LeetCode.225.用队列实现栈
算法
Elaine3365 小时前
数据结构与算法-程序
数据结构·python·算法·计算机基础·编程基础
2601_965742226 小时前
布局GEO AI本地营销,我重点看这几个细节
大数据·人工智能·算法·ai·新媒体运营
EatFan6 小时前
AI Agent 上生产前先加三道闸门:审批、限权、可回放的工程实践
人工智能·python·算法·多智能体·ai agent·mcp·harness
思茂信息6 小时前
CST软件BCI仿真模型及仿真案例
开发语言·单片机·嵌入式硬件·算法·emc
Persistent的粽子!6 小时前
双指针算法:最大盛水容器
c++·算法·leetcode