Skip to content

Commit b7a90e5

Browse files
authored
Merge pull request #1297 from Hemav009/141
Added leetcode solution for 141
2 parents e094e6b + 42257b5 commit b7a90e5

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
id: linked-list-cycle
3+
title: Linked List Cycle
4+
sidebar_label: 0141- Linked List Cycle
5+
tags:
6+
- DSA
7+
- Leetcode
8+
- Linked List
9+
10+
description: "This is a solution to the Linked List cycle on LeetCode."
11+
---
12+
13+
## Problem Statement
14+
15+
Given head, the head of a linked list, determine if the linked list has a cycle in it.
16+
17+
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.
18+
19+
Return true if there is a cycle in the linked list. Otherwise, return false.
20+
21+
### Examples
22+
23+
**Example 1:**
24+
25+
```
26+
Input: head = [3,2,0,-4], pos = 1
27+
Output: true
28+
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: head = [1,2], pos = 0
35+
Output: true
36+
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
37+
```
38+
39+
**Example 3:**
40+
41+
```
42+
Input: head = [1], pos = -1
43+
Output: false
44+
Explanation: There is no cycle in the linked list.
45+
```
46+
47+
### Constraints:
48+
49+
- The number of the nodes in the list is in the range $[0, 10^4].$
50+
- $-105 <= Node.val <= 105$
51+
- $pos is -1 or a valid index in the linked-list.$
52+
53+
## Algorithm
54+
55+
1. **Initialization**:
56+
57+
- Initialize two pointers, `slow` and `fast`, both pointing to the head of the linked list.
58+
59+
2. **Traversal**:
60+
61+
- Move the `slow` pointer one step at a time.
62+
- Move the `fast` pointer two steps at a time.
63+
64+
3. **Cycle Detection**:
65+
66+
- If there is a cycle, the `fast` pointer will eventually meet the `slow` pointer within the cycle.
67+
- If there is no cycle, the `fast` pointer will reach the end of the list (NULL).
68+
69+
4. **Return Result**:
70+
- If the `fast` pointer meets the `slow` pointer, return `true` indicating a cycle is detected.
71+
- If the `fast` pointer reaches the end of the list, return `false` indicating no cycle is detected.
72+
73+
## Code Implementation
74+
75+
### C++
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
bool hasCycle(ListNode *head) {
81+
ListNode* fast = head;
82+
ListNode* slow = head;
83+
84+
while (fast != NULL && fast->next != NULL) {
85+
fast = fast->next->next;
86+
slow = slow->next;
87+
if (fast == slow) {
88+
return true;
89+
}
90+
}
91+
92+
return false;
93+
}
94+
};
95+
```
96+
97+
### Python
98+
99+
```python
100+
class ListNode:
101+
def __init__(self, x):
102+
self.val = x
103+
self.next = None
104+
105+
class Solution:
106+
def hasCycle(self, head: ListNode) -> bool:
107+
fast = head
108+
slow = head
109+
110+
while fast is not None and fast.next is not None:
111+
fast = fast.next.next
112+
slow = slow.next
113+
if fast == slow:
114+
return True
115+
116+
return False
117+
```
118+
119+
### Java
120+
121+
```java
122+
public class Solution {
123+
public boolean hasCycle(ListNode head) {
124+
ListNode fast = head;
125+
ListNode slow = head;
126+
127+
while (fast != null && fast.next != null) {
128+
fast = fast.next.next;
129+
slow = slow.next;
130+
if (fast == slow) {
131+
return true;
132+
}
133+
}
134+
135+
return false;
136+
}
137+
}
138+
```
139+
140+
### JavaScript
141+
142+
```javascript
143+
var hasCycle = function (head) {
144+
let fast = head;
145+
let slow = head;
146+
147+
while (fast !== null && fast.next !== null) {
148+
fast = fast.next.next;
149+
slow = slow.next;
150+
if (fast === slow) {
151+
return true;
152+
}
153+
}
154+
155+
return false;
156+
};
157+
```

0 commit comments

Comments
 (0)