`

Linked List Cycle

阅读更多
Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

判断一个链表中是否有环存在,用快慢指针来解决,设定两个指针fast和slow都指向head,我们让fast = fast.next.next,slow = slow.next,如果存在环,fast和slow就会相遇,因此当fast == slow时我们就返回true, 否则返回false。代码如下:
/**
 * 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 fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast)
                return true;
        }
        return false;
    }
}
分享到:
评论

相关推荐

    cpp-算法精粹

    Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary Longest Palindromic Substring Regular Expression ...

    leetcode不会-Leetcode-Java:Leetcode-Java

    Linked List Linked List Cycle Given a linked list, determine if it has a cycle in it. public static boolean hasCycle(ListNode head) 快慢指针法,块指针从head.next开始,慢指针从head开始,快指针每次移动...

    leetcode2sumc-LeetCode:LeetCode的一些题目

    leetcode 2 sum c LeetCode 帮助文档 ...Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List

    leetcode中文版-LeetCode:力码

    List LeetCode 92.Reverse Linked List II LeetCode 138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160.Intersection of Two ...

    javalruleetcode-leetcode-java:力码笔记

    java lru leetcode leetcode-java leetcode刷题笔记 ...141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV 217.Contains Duplicate 263.Ugly Number 264.Ugly Number II

    java二叉树算法源码-algorithm-primer:algorithmprimer-算法基础、Leetcode编程和剑指offer,Ja

    java二叉树算法源码 algorithm-primer algorithm ...List Medium 21 合并两个有序链表 Merge Two Sorted Lists Easy 141 判断链表是是否存在环 Linked List Cycle Easy 142 环形链表II Linked List Cycle I

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    lrucacheleetcode-LeetCode:LeetCode刷题

    环形链表(Linked List Cycle) 2018.9.25 环形链表 II(Linked List Cycle II) 2018.9.26 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) ...

    leetcode卡-leetcode:利特码解决方案

    Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要...

    tech.github.io:我的博客

    终生成长 :hot_beverage: 为什么要建这个仓库 梳理自己掌握的知识点,整理自己的知识体系。 I Hear and I Forget, I See and I ... Linked List CycleLeetcode 21. Merge Two Sorted ListsLeetCode 224. Basic Cal

    leetcode和oj-leetcode_downloader:用于您已接受的leetcodeoj提交的下载器

    linked-list-cycle/Solution.993783.java $ tree . ├── add-binary │  └── Solution.665166.java ├── add-two-numbers │  └── Solution.666385.java ├── balanced-binary-tree │  └── ...

    leetcode怎么销号-LeetCode-Top-Interview-Questions:LeetCode-Top-Interview-Qu

    Linked List Cycle 问题描述 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解决思路 声明一个慢指针和一个快指针 慢指针一次后移一个节点,快...

    leetcode中国-leetcode:刷算法了

    Linked List Cycle 快慢指针 Linked List Cycle II 快慢指针再加个初始指针 慢指针到链表开始位置时, 快指针总是比他快k步(每次移动快1步, 移动k次), 第一次相遇的时候快慢指针位置距离链表起始位置差k步即在n-k的...

    leetcode2sumc--Offer:-提供

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类...Cycle 160 * Intersection of Two Linke

    leetcode2sumc-ack-CherishLeetCode:ack-CherishLeetCode

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类...Cycle 160 * Intersection of Two Linke

    javalruleetcode-JavaInterviewChecklist:要检查的Java事项

    Cycle Remove Nth Node From End of List Merge Two Sorted Lists 两个链表的交集 Remove Duplicates from Sorted List Palindrome Linked List LL中的插入排序 使用额外的缓冲区从未排序的链表中删除重复项 细绳 ...

    leetcode答案-LeetCodeSolution:这是LeetCode网站问题的解决方案集

    Linked List Cycle 判断链表是否有环。通过快慢节点可以简单实现。 Unique Binary Search Trees 本题参考了 里面的*How Many Binary Trees Are There?*章节。 The first few terms: C(0) = 1 C(1) = C(0)C(0) = 1 C...

    leetcode2sumc-DataWhale_exercise:用C++编程

    leetcode 2 sum c DataWhale_exercise programming in c++ Task 1:数组和链表(2天)【当前任务】 时间:2019-08-03 21:00 - 2019-08-05 21:00 讨论&CR时间: 2019-08-05 ...Linked List Cycle I(环形链

    leetcode答案-code_challenges:代码挑战

    leetcode 答案Leet Code 挑战 这是我提交给 Lambda School CS Unit 2 构建周的已接受 ...如果您想自己尝试,这些链接将带您进入代码挑战。...要查看我接受的答案,只需转到与...[Linked List Cycle II](https://leetcode.co

    validnumberleetcode自动机-LearingTripofNoah:我自己的学习之旅,天天学习至此方休

    Linked List Cycle I(环形链表) 英文版: 中文版: Merge k Sorted Lists(合并 k 个排序链表) 英文版: 中文版: Task2(8.5-8.9) 3.栈 用数组实现一个顺序栈 用链表实现一个链式栈 编程模拟实现一个浏览器的...

Global site tag (gtag.js) - Google Analytics