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
}
相关推荐
无尽的罚坐人生几秒前
hot 100 560.和为 K 的子数组
数据结构·算法·leetcode
Ll13045252983 分钟前
leetcode代码随想录数组篇
数据结构·算法·leetcode
一个平凡而乐于分享的小比特14 分钟前
ARRAY_SIZE宏作用及使用注意事项
c语言·array_size·内核宏
Remember_99325 分钟前
【LeetCode精选算法】位运算专题一
java·开发语言·数据结构·leetcode·哈希算法
MicroTech202529 分钟前
微算法科技(NASDAQ :MLGO)量子生成对抗网络(QGAN)技术,为网络安全防御提供了全新的技术路径
科技·算法·生成对抗网络
leaves falling31 分钟前
c语言-编译和链接
c语言·开发语言
YuTaoShao32 分钟前
【LeetCode 每日一题】3507. 移除最小数对使数组有序 I
算法·leetcode·职场和发展
VekiSon40 分钟前
ARM架构——UART 串口通信详解
c语言·arm开发·单片机·嵌入式硬件
LYS_06181 小时前
RM赛事C型板九轴IMU解算(3)(姿态融合算法)
c语言·算法·imu·姿态解算·四元数到欧拉角
LDG_AGI1 小时前
【机器学习】深度学习推荐系统(三十一):X For You Feed 全新推荐系统技术架构深度解析
人工智能·深度学习·算法·机器学习·架构·推荐算法