LeetCode 141 Linked List Cycle

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

LeetCode 141

Linked List Cycle

  • Problem Description:
    查看链表中是否存在环。需要注意的是,题目问的不是该链表是否是循环链表,而是问链表中有木有环

    具体的题目信息:
    https://leetcode.com/problems/linked-list-cycle/description/

  • Solution:

    • 解题思路
      我们用INT_MIN来记录链表中访问过的结点,当遍历中发现结点的val值为INT_MIN时,说明该结点已经访问过,故原链表存在环。
    • 编程实现
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if (head == NULL)
            return false;
        if (head->next == NULL)
            return false;
        ListNode* q = head;
        while(q) {
            if (q->val != INT_MIN) {
                q->val = INT_MIN;
                q = q->next;
            } else {
                return true;
            }
        }
        return false;
    }
};
LeetCode OJ 141. Linked List Cycle LeetCode OJ 141. Linked List CycleDescriptionGiven a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?解题思路对于判断环的问题,可以采用双指针(快慢指针)的方法来判断。另外,因为题目中给的Lis 阅读详情

相关推荐

LeetCode力扣 141. 环形链表 Linked List Cycle 题解代码 JavaScript

问题https://leetcode-cn.com/problems/valid-palindrome/ 练习使用JavaScript解答 /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} head * @return {boolean} */.

Coding home - 漂流瓶jz 522

[LeetCode] 141. Linked List Cycle

原题链接: https://leetcode.com/problems/linked-list-cycle/ 1. 题目介绍 Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which repres...

Ber03的博客 578

Leetcode 141 Linked List Cycle

Leetcode 141 Linked List Cycle 题目描述 Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe...

qq_43271202的博客 225

LeetCode 141. Linked List Cycle 题解

本题是链表环检测的经典问题。哈希表记录访问过的节点快慢指针(龟兔赛跑算法)时间和空间复杂度的权衡通过本题可以深入理解链表操作和环检测算法。

cannonjinx的博客 3069

LeetCode 141. Linked List Cycle(环形链表) -- c语言

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integerposwhich represents the position (0-indexed)in the l...

d_benhua的博客 372

LeetCode--LinkedList--141.Linked List Cycle(Easy)

141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we us...

weixin_30235225的博客 162

Leetcode 141:Linked List Cycle python

Leetcode 141:Linked List Cycle题目解法1解法2 题目 Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-i...

qq_37821701的博客 901

Leetcode 141: Linked List Cycle

Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail con...

HeisenbergTHUUCD的博客 240

LeetCode 141. Linked List Cycle

欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-141-linked-list-cycle/ LeetCode 141. // 链表的结构 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; *...

user2639的博客 181

力扣(LeetCode141.环形链表(Linked List Cycle

Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to deno

s_w_s_的博客 229

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? 题目分析: 最常见的想法是遍历linked list,同时用个set进行记录遍历过的节点,如果遍历linked list时发现当前节点已经在set中出现

BUILD 3730

LeetCode-141. Linked List Cycle [C++][Java]

Givenhead, the head of a linked list, determine if the linked list has a cycle in it.

贫道绝缘子的博客 310

141. Linked List Cycle【力扣】

题意理解 判断一个链表中是否有环 问题分析 设计一个hash表,key为链表结点地址,value为结点位置(从1开始)。遍历链表,如果结点不在hash表中,存入结点地址和位置;如果在hash表中,说明有环。遍历结束,说明无环。 其他 空间复杂度0(1)的算法,设置两个指针,一个每次循环走两步,一个每次循环走一步,如果走两步的每一步到了结尾,返回无环;如果走两步的位置和走一步的位置相等,说...

xiexie1357的专栏 223

LeetCode-141 Linked List Cycle

Description Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is

秦智慧的博客 332

[LeetCode]141. Linked List Cycle

                                            [LeetCode]141. Linked List Cycle                                                                                           2018-8-12 1.题目描述 Given a linke...

左左张 1016

Leetcode-141. Linked List Cycle

Hash Table、LinkedList、Two Pointers

Eistert 327

LeetCode141Linked List Cycle

本题思路是定义两个指针,快指针和慢指针。快指针一次跳两个节点,慢指针一次跳一个节点。如果有环那么两个指针会相遇,如果无环那么快指针会提前指向null,所以可以做一个while循环,条件是快慢指针的下一个节点不为空,这里有一个问题需要注意,如果一个指针的下一个节点为空那么p.next.next放在while循环中会抛出指向为空的错误,所以需要在while中再加一个if判断。 /** * De...

Juliussss 270

Leetcode141. Linked List Cycle

题目地址: https://leetcode.com/problems/linked-list-cycle/ 链表求环,返回环是否存在。经典的快慢指针算法。如果链表没有node,或者链表只有一个node且其未指向自己,则返回false。否则设快慢指针slow和fast初始化为head,slow每次走一步,fast每次走两步,如果某一时刻fast = null或者fast.next = null,说...

数学、算法爱好者的博客 198
上一篇: LeetCode 160 Intersection of Two Linked Lists
下一篇: LeetCode 21 Merge Two Sorted Lists
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值