LeetCode //C - 141. Linked List Cycle

141. 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 denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. Otherwise, return false.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Constraints:

  • The number of the nodes in the list is in the range [ 0 , 1 0 4 ] [0, 10^4] [0,104].
  • − 1 0 5 < = N o d e . v a l < = 1 0 5 -10^5 <= Node.val <= 10^5 −105<=Node.val<=105
  • pos is -1 or a valid index in the linked-list.

From: LeetCode

Link: 141. Linked List Cycle


Solution:

Ideas:

Fundamental Idea:

Imagine two runners on a circular track, one runner (the hare) is much faster than the other (the tortoise). If they start at the same position and run in the same direction, the faster runner (hare) will eventually lap the slower runner (tortoise). Similarly, in a linked list, if there is a cycle, a faster pointer will eventually meet the slower pointer within the cycle.

Code Explanation:

1. Initialization:

  • We have two pointers: tortoise (slow-moving) and hare (fast-moving). Both start at the head of the linked list.

2. Movement:

  • The tortoise moves one step at a time (tortoise = tortoise->next).
  • The hare moves two steps at a time (hare = hare->next->next).

3. Checking for Cycle:

  • If there is no cycle in the linked list, the hare (which moves faster) will eventually reach the end of the list and encounter a NULL pointer.
  • If there is a cycle, the hare will eventually "lap" the tortoise, and they will meet at some point inside the cycle.

4. Loop Termination:

  • The loop continues as long as hare and hare->next are not NULL.
  • If tortoise and hare pointers meet (tortoise == hare), it indicates the presence of a cycle, and the function returns true.
  • If the loop ends without the pointers meeting, there is no cycle, and the function returns false.
Code:
c 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

bool hasCycle(struct ListNode *head) {
    if (!head) return false; // If list is empty
    
    struct ListNode *tortoise = head;  // Slow pointer
    struct ListNode *hare = head;      // Fast pointer

    while (hare != NULL && hare->next != NULL) {
        tortoise = tortoise->next;          // Move slow pointer one step
        hare = hare->next->next;            // Move fast pointer two steps
        
        if (tortoise == hare) return true;  // If they meet, there's a cycle
    }

    return false;  // If loop exits, there's no cycle
}
相关推荐
源代码•宸几秒前
分布式缓存-GO(分布式算法之一致性哈希、缓存对外服务化)
开发语言·经验分享·分布式·后端·算法·缓存·golang
yongui4783439 分钟前
MATLAB的指纹识别系统实现
算法
高山上有一只小老虎41 分钟前
翻之矩阵中的行
java·算法
jghhh011 小时前
RINEX文件进行卫星导航解算
算法
爱思德学术1 小时前
中国计算机学会(CCF)推荐学术会议-A(计算机科学理论):LICS 2026
算法·计算机理论·计算机逻辑
CVHub1 小时前
多模态图文训推一体化平台 X-AnyLabeling 3.0 版本正式发布!首次支持远程模型推理服务,并新增 Qwen3-VL 等多款主流模型及诸多功能特性,等
算法
hoiii1871 小时前
MATLAB实现Canny边缘检测算法
算法·计算机视觉·matlab
qq_430855882 小时前
线代第二章矩阵第四课:方阵的幂
算法·机器学习·矩阵
神圣的大喵2 小时前
平台无关的嵌入式通用按键管理器
c语言·单片机·嵌入式硬件·嵌入式·按键库
roman_日积跬步-终至千里2 小时前
【计算机设计与算法-习题2】动态规划应用:矩阵乘法与钢条切割问题
算法·矩阵·动态规划