Skip to content

Commit bd92075

Browse files
authored
Merge pull request #1300 from Hemav009/142
Added leetcode solution for 142
2 parents b7a90e5 + bbbf223 commit bd92075

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
id: linked-list-cycle--II
3+
title: Linked List Cycle II
4+
sidebar_label: 0142- Linked List Cycle II
5+
tags:
6+
- DSA
7+
- Leetcode
8+
- Linked List
9+
10+
description: "This is a solution to the Linked List cycle II on LeetCode."
11+
---
12+
13+
## Problem Statement
14+
15+
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
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 (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.
18+
19+
Do not modify the linked list.
20+
21+
### Examples
22+
23+
**Example 1:**
24+
25+
```
26+
Input: head = [3,2,0,-4], pos = 1
27+
Output: tail connects to node index 1
28+
Explanation: There is a cycle in the linked list, where tail connects to the second node.
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: head = [1,2], pos = 0
35+
Output: tail connects to node index 0
36+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
37+
```
38+
39+
**Example 3:**
40+
41+
```
42+
Input: head = [1], pos = -1
43+
Output: no cycle
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+
- $-10^5 <= Node.val <= 10^5$
51+
- pos is `-1` or a valid index in the linked-list.
52+
53+
## Algorithm for Detecting the Start of a Cycle in a Linked List
54+
55+
1. **Initialization**:
56+
57+
- Initialize two pointers, `slow` and `fast`, both pointing to the head of the linked list.
58+
59+
2. **Cycle Detection**:
60+
61+
- Move the `slow` pointer one step at a time.
62+
- Move the `fast` pointer two steps at a time.
63+
- If there is a cycle, the `fast` pointer will eventually meet the `slow` pointer within the cycle.
64+
65+
3. **Finding the Cycle Start**:
66+
67+
- If the `fast` pointer meets the `slow` pointer, reset the `slow` pointer to the head of the list.
68+
- Move both pointers one step at a time until they meet again.
69+
- The node where they meet is the start of the cycle.
70+
71+
4. **Return Result**:
72+
- If the `fast` pointer meets the `slow` pointer, return the meeting node.
73+
- If there is no cycle, return `NULL`.
74+
75+
## Code Implementations
76+
77+
### C++
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
ListNode *detectCycle(ListNode *head) {
83+
ListNode* fast = head;
84+
ListNode* slow = head;
85+
86+
while (fast != NULL && fast->next != NULL) {
87+
slow = slow->next;
88+
fast = fast->next->next;
89+
if (slow == fast) {
90+
slow = head;
91+
while (slow != fast) {
92+
slow = slow->next;
93+
fast = fast->next;
94+
}
95+
return slow;
96+
}
97+
}
98+
return NULL;
99+
}
100+
};
101+
```
102+
103+
### Python
104+
105+
```python
106+
class ListNode:
107+
def __init__(self, x):
108+
self.val = x
109+
self.next = None
110+
111+
class Solution:
112+
def detectCycle(self, head: ListNode) -> ListNode:
113+
fast = head
114+
slow = head
115+
116+
while fast is not None and fast.next is not None:
117+
slow = slow.next
118+
fast = fast.next.next
119+
if slow == fast:
120+
slow = head
121+
while slow != fast:
122+
slow = slow.next
123+
fast = fast.next
124+
return slow
125+
return None
126+
```
127+
128+
### Java
129+
130+
```java
131+
public class Solution {
132+
public ListNode detectCycle(ListNode head) {
133+
ListNode fast = head;
134+
ListNode slow = head;
135+
136+
while (fast != null && fast.next != null) {
137+
slow = slow.next;
138+
fast = fast.next.next;
139+
if (slow == fast) {
140+
slow = head;
141+
while (slow != fast) {
142+
slow = slow.next;
143+
fast = fast.next;
144+
}
145+
return slow;
146+
}
147+
}
148+
return null;
149+
}
150+
}
151+
```
152+
153+
### JavaScript
154+
155+
```javascript
156+
var detectCycle = function (head) {
157+
let fast = head;
158+
let slow = head;
159+
160+
while (fast !== null && fast.next !== null) {
161+
slow = slow.next;
162+
fast = fast.next.next;
163+
if (slow === fast) {
164+
slow = head;
165+
while (slow !== fast) {
166+
slow = slow.next;
167+
fast = fast.next;
168+
}
169+
return slow;
170+
}
171+
}
172+
return null;
173+
};
174+
```

0 commit comments

Comments
 (0)