Skip to content

Commit 9faca97

Browse files
authored
Merge pull request #1453 from PradnyaGaitonde/PradnyaGaitonde-patch-10
Create 0141-linked-list-cycles.md
2 parents 632bae2 + a0003bc commit 9faca97

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
id: linked-list-cycle
3+
title: Linked List Cycle(LeetCode)
4+
sidebar_label: 0141-Linked List Cycle
5+
tags:
6+
- Linked list
7+
- Hash Table
8+
- Two pointer
9+
description: Given head, the head of a linked list, determine if the linked list has a cycle in it.
10+
---
11+
12+
## Problem Statement
13+
14+
Given `head`, the head of a linked list, determine if the linked list has a cycle in it.
15+
16+
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.
17+
18+
Return `true` if there is a cycle in the linked list. Otherwise, return `false`.
19+
20+
### Examples
21+
22+
**Example 1:**
23+
24+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/7da67d86-c090-49ad-9ed1-7e342a0e650a)
25+
26+
```plaintext
27+
Input: head = [3,2,0,-4], pos = 1
28+
Output: true
29+
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
30+
```
31+
32+
**Example 2:**
33+
34+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/9d7aa2ed-77b7-4f92-b6da-2360bb1c6cd1)
35+
36+
```plaintext
37+
Input: head = [1,2], pos = 0
38+
Output: true
39+
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
40+
```
41+
42+
**Example 3:**
43+
44+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/b0f0c2aa-747e-4c74-975b-a68cc61cafb7)
45+
46+
```plaintext
47+
Input: head = [1], pos = -1
48+
Output: false
49+
Explanation: There is no cycle in the linked list.
50+
```
51+
52+
### Constraints
53+
54+
- The number of the nodes in the list is in the range `[0, 104]`.
55+
- `-105 <= Node.val <= 105`
56+
- `pos` is `-1` or a valid index in the linked-list.
57+
58+
## Solution
59+
60+
This algorithm, also known as Floyd's Cycle-Finding Algorithm or the "tortoise and the hare algorithm," is used to detect cycles in a linked list. It uses two pointers, one moving at twice the speed of the other, to detect if the linked list contains a cycle.
61+
62+
### Approach
63+
64+
#### Algorithm
65+
66+
1. Initialize two pointers, `slow` and `fast`, pointing to the head of the linked list.
67+
2. Move `slow` one step at a time and `fast` two steps at a time.
68+
3. If `fast` reaches the end of the list (i.e., `fast` or `fast->next` is `NULL`), then there is no cycle, so return false.
69+
4. If `fast` meets `slow` at any point during traversal, then there is a cycle in the linked list, so return true.
70+
71+
#### Implementation
72+
73+
```C++
74+
class Solution {
75+
public:
76+
bool hasCycle(ListNode *head) {
77+
ListNode *fast = head;
78+
ListNode *slow = head;
79+
80+
// Traverse the linked list
81+
while (fast != NULL && fast->next != NULL) {
82+
fast = fast->next->next; // Move fast pointer two steps
83+
slow = slow->next; // Move slow pointer one step
84+
85+
// Check if fast and slow pointers meet
86+
if (fast == slow) {
87+
return true; // Cycle detected
88+
}
89+
}
90+
91+
return false; // No cycle found
92+
}
93+
};
94+
```
95+
96+
### Complexity Analysis
97+
98+
- **Time complexity**: The slow pointer traverses the list once, and the fast pointer traverses it twice. Therefore, the time complexity is O(N), where N is the number of nodes in the linked list.
99+
- **Space complexity**: The algorithm uses only two pointers (`slow` and `fast`) and has a constant space complexity of O(1).
100+
101+
### Conclusion
102+
103+
The Floyd's Cycle-Finding Algorithm is an efficient way to detect cycles in a linked list using only two pointers and constant space. It is a popular algorithm due to its simplicity and effectiveness in solving this problem.

0 commit comments

Comments
 (0)