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;
    }
}
相关推荐
Orlando cron27 分钟前
数据结构入门:链表
数据结构·算法·链表
牛客企业服务1 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
糖葫芦君2 小时前
Policy Gradient【强化学习的数学原理】
算法
向阳@向远方4 小时前
第二章 简单程序设计
开发语言·c++·算法
github_czy4 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁5 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子5 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z6 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao6 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx6 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法