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;
    }
}
相关推荐
Storynone几秒前
【Day26】LeetCode:452. 用最少数量的箭引爆气球,435. 无重叠区间,763. 划分字母区间
python·算法·leetcode
月明长歌2 分钟前
【码道初阶-Hot100】LeetCode 3. 无重复字符的最长子串:从错误直觉到滑动窗口,彻底讲透为什么必须判断 `map.get(c) >= left`
java·算法·leetcode·哈希算法
junnhwan5 分钟前
LeetCode Hot 100——贪心算法
java·算法·leetcode
魑魅魍魉都是鬼6 分钟前
java 的排序算法
java·算法·排序算法
2401_853576506 分钟前
并行算法在STL中的应用
开发语言·c++·算法
晓纪同学7 分钟前
ROS2 -06-动作
java·数据库·python·算法·机器人·ros·ros2
无限进步_7 分钟前
【C++】字符串中的字母反转算法详解
开发语言·c++·ide·git·算法·github·visual studio
qyzm7 分钟前
Codeforces Round 927 (Div. 3)
数据结构·python·算法
2401_891482179 分钟前
C++中的状态模式实战
开发语言·c++·算法
Frostnova丶11 分钟前
LeetCode 1727.重新排列后的最大子矩阵
算法·leetcode