`

LeetCode 141 - 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?

 

Solution 1:

public boolean hasCycle(ListNode head) {
    ListNode walker = head, runner = head;
    do {
        if(runner == null || runner.next == null) 
            return false;
        walker = walker.next;
        runner = runner.next.next;
    } while(walker != runner);
    return true;
}

 

Solution 2:

public boolean hasCycle(ListNode head) {
    if(head == null) return false;
    ListNode walker = head, runner = head.next;
    while(runner != null) {
        if(walker == runner) return true;
        walker = walker.next;
        runner = runner.next;
        if(runner != null) 
            runner = runner.next;
    } 
    return false;
}

 

分享到:
评论

相关推荐

    python-leetcode题解之141-Linked-List-Cycle

    在解决“Python LeetCode题解之141-Linked-List-Cycle”这一问题时,我们首先需要了解链表的基本概念,包括单向链表和循环链表的定义及其特点。单向链表是一种由一系列节点组成的线性数据结构,每个节点都包含数据...

    js-leetcode题解之141-linked-list-cycle.js

    在JavaScript中解决LeetCode上的141号问题“环形链表”,是一种常见的算法练习题,旨在检测程序员对数据结构特别是链表的理解程度以及编程能力。这一题的核心在于判断给定的链表是否有环,并返回布尔值。掌握如何...

    python-leetcode题解之142-Linked-List-Cycle-II

    "python-leetcode题解之142-Linked-List-Cycle-II"不仅是对一个具体问题的解决,也是对数据结构和算法知识的一个全面复习和应用。通过解决这个问题,我们不仅能够加深对链表结构和快慢指针算法的理解,还可以提升...

    js-leetcode题解之142-linked-list-cycle-ii.js

    在JavaScript中解决LeetCode的“142.环形链表II”问题涉及到对链表操作的理解,特别是检测链表中是否存在环,并找出环的起始位置。这个问题是链表问题中的一个经典问题,经常作为面试题出现,考察应聘者对数据结构和...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II ...

    leetcode不会-Leetcode-Java:Leetcode-Java

    leetcode 不会 Leetcode Solutions in 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....

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

    leetcode怎么销号 LeetCode便签 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:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的括号:valid-parentheses 150-逆波兰表达式求值:evaluate-reverse-polish-...

    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

    leetcode-链表笔记

    Linked List Cycle**:判断链表是否有环,如有,找到环的入口节点。 - **19. Remove Nth From End**:移除链表中的第 n 个节点。 - **206. Reverse Linked List**:反转整个链表。 - **21. Merge Two Sorted ...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to...

    leetcode中325题python-leetcode:leetcode

    leetcode中325题python leetcode 以 参考 和 Hash相关 1_两数之和 ...linked-list-cycle-ii 143 重排链表 reorder-list 148 排序链表 sort-list 234 回文链表 palindrome-linked-list 双指针遍历/滑动

    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 其中算法,主要...

    leetcode分类-LeetCodeJourney:保持练习

    141.Linked-List-Cycle │  ├── solution.cpp │  ├── solution.go │  └── solution.md [ -> Explain thoughts about this problem.] ├── 404.Sum-of-Left-Leaves │  ├── solution.cpp │  ├...

    leetcode中国-leetcode:刷算法了

    leetcode中国 Leetcode Set Matrix Zeroes 第一种 通过一次遍历记录所有为0的索引(Python中enumerate()输出当前列表的索引) 再遍历一次, 根据记录的索引进行置0 第二种 通过一次遍历所有为0的索引, 设置当前索引的...

    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...

    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]...

    leetcode答案-code_challenges:代码挑战

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

    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

Global site tag (gtag.js) - Google Analytics