Skip to content

Commit c6cc576

Browse files
Leetcode 142
1 parent 3cc1f2c commit c6cc576

1 file changed

Lines changed: 94 additions & 43 deletions

File tree

Leetcode/Leetcode_142.py

Lines changed: 94 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
=========================================================
55
66
Problem:
7-
Given the head of a linked list, return the node where the cycle begins.
8-
If there is no cycle, return None.
7+
Given the head of a linked list, return the node where the cycle
8+
begins. If there is no cycle, return None.
99
1010
A cycle exists in a linked list if some node can be reached again
1111
by continuously following the next pointer.
1212
13-
You must solve it using O(1) extra space.
14-
1513
---------------------------------------------------------
16-
Example 1:
14+
Example 1
15+
1716
Input:
1817
head = [3,2,0,-4], pos = 1
1918
20-
Visual Representation:
19+
Visual:
2120
3 -> 2 -> 0 -> -4
2221
^ |
2322
|_________|
@@ -26,15 +25,16 @@
2625
Node with value 2
2726
2827
Explanation:
29-
The tail connects to the node at index 1,
30-
so the cycle starts at node 2.
28+
The tail connects to the node at index 1, so the cycle starts
29+
at node 2.
3130
3231
---------------------------------------------------------
33-
Example 2:
32+
Example 2
33+
3434
Input:
3535
head = [1,2], pos = 0
3636
37-
Visual Representation:
37+
Visual:
3838
1 -> 2
3939
^ |
4040
|____|
@@ -43,38 +43,60 @@
4343
Node with value 1
4444
4545
---------------------------------------------------------
46-
Example 3:
46+
Example 3
47+
4748
Input:
4849
head = [1], pos = -1
4950
50-
Visual Representation:
51-
1 -> None
52-
5351
Output:
5452
None
5553
5654
---------------------------------------------------------
57-
Approach (Floyd's Cycle Detection / Tortoise and Hare)
55+
Approach 1: Hash Set
56+
---------------------------------------------------------
57+
58+
Idea:
59+
Store every visited node in a hash set.
60+
61+
Steps:
62+
1. Traverse the linked list.
63+
2. If the current node is already present in the set,
64+
return that node.
65+
3. Otherwise, add it to the set.
66+
4. If we reach None, no cycle exists.
67+
68+
Why it works:
69+
- The first node visited twice is the starting node
70+
of the cycle.
71+
72+
Time Complexity:
73+
O(n)
74+
75+
Space Complexity:
76+
O(n)
77+
78+
---------------------------------------------------------
79+
Approach 2: Floyd's Cycle Detection (Tortoise & Hare)
80+
---------------------------------------------------------
81+
82+
Phase 1: Detect Cycle
5883
59-
Step 1: Detect whether a cycle exists.
6084
- Use two pointers:
61-
slow -> moves one step at a time
62-
fast -> moves two steps at a time
85+
slow -> moves 1 step
86+
fast -> moves 2 steps
6387
64-
- If there is no cycle:
65-
fast reaches None.
88+
- If there is a cycle, they will eventually meet.
89+
- If fast reaches None, no cycle exists.
6690
67-
- If there is a cycle:
68-
slow and fast eventually meet.
91+
Phase 2: Find Cycle Start
6992
70-
Step 2: Find the starting node of the cycle.
7193
- Reset slow to head.
7294
- Keep fast at the meeting point.
73-
- Move both pointers one step at a time.
74-
- The node where they meet again is the
75-
starting node of the cycle.
95+
- Move both one step at a time.
96+
- The node where they meet is the start of the cycle.
7697
7798
Why does this work?
99+
78100
Let:
79101
x = distance from head to cycle start
80102
y = distance from cycle start to meeting point
@@ -88,30 +110,21 @@
88110
89111
=> x = k*c - y
90112
91-
This means the distance from head to cycle start
92-
is equal to the distance from meeting point to
113+
This means the distance from the head to the cycle start
114+
is equal to the distance from the meeting point to the
93115
cycle start.
94116
95-
Therefore, moving both pointers one step at a time
96-
makes them meet exactly at the cycle's starting node.
117+
Therefore, moving both pointers one step at a time makes
118+
them meet exactly at the cycle's starting node.
97119
98-
---------------------------------------------------------
99120
Time Complexity:
100121
O(n)
101122
102-
- First phase (cycle detection): O(n)
103-
- Second phase (finding cycle start): O(n)
104-
105-
Overall: O(n)
106-
107-
---------------------------------------------------------
108123
Space Complexity:
109124
O(1)
110125
111-
Only two pointers are used.
112-
113126
---------------------------------------------------------
114-
Python Solution
127+
Python Solution - Hash Set
115128
---------------------------------------------------------
116129
"""
117130

@@ -123,10 +136,31 @@
123136

124137
from typing import Optional
125138

139+
class Solution:
140+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
141+
visited = set()
142+
curr = head
143+
144+
while curr:
145+
if curr in visited:
146+
return curr
147+
148+
visited.add(curr)
149+
curr = curr.next
150+
151+
return None
152+
153+
154+
"""
155+
---------------------------------------------------------
156+
Python Solution - Floyd's Cycle Detection
157+
---------------------------------------------------------
158+
"""
159+
126160
class Solution:
127161
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
128162

129-
# Phase 1: Detect cycle
163+
# Phase 1: Detect Cycle
130164
slow = head
131165
fast = head
132166

@@ -139,11 +173,28 @@ def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
139173
else:
140174
return None
141175

142-
# Phase 2: Find cycle start
176+
# Phase 2: Find Start of Cycle
143177
slow = head
144178

145179
while slow != fast:
146180
slow = slow.next
147181
fast = fast.next
148182

149-
return slow
183+
return slow
184+
185+
186+
"""
187+
=========================================================
188+
Comparison
189+
=========================================================
190+
191+
Hash Set:
192+
- Time Complexity: O(n)
193+
- Space Complexity: O(n)
194+
- Easy to understand and implement.
195+
196+
Floyd's Cycle Detection:
197+
- Time Complexity: O(n)
198+
- Space Complexity: O(1)
199+
- Optimal solution and expected in interviews.
200+
"""

0 commit comments

Comments
 (0)