Skip to content

Commit 852e544

Browse files
committed
[LeetCode Sync] Runtime - 36 ms (96.38%), Memory - 19.6 MB (80.23%)
1 parent fe17112 commit 852e544

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
2+
3+
<p>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 <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail&#39;s <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
4+
5+
<p><strong>Do not modify</strong> the linked list.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
10+
<pre>
11+
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
12+
<strong>Output:</strong> tail connects to node index 1
13+
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
18+
<pre>
19+
<strong>Input:</strong> head = [1,2], pos = 0
20+
<strong>Output:</strong> tail connects to node index 0
21+
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
22+
</pre>
23+
24+
<p><strong class="example">Example 3:</strong></p>
25+
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
26+
<pre>
27+
<strong>Input:</strong> head = [1], pos = -1
28+
<strong>Output:</strong> no cycle
29+
<strong>Explanation:</strong> There is no cycle in the linked list.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
37+
<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
38+
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
39+
</ul>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
9+
fast = slow = head
10+
while fast and fast.next:
11+
slow = slow.next
12+
fast = fast.next.next
13+
if slow == fast:
14+
result = head
15+
while result != slow:
16+
result = result.next
17+
slow = slow.next
18+
return result
19+
20+
# visited = set()
21+
# current = head
22+
# while current:
23+
# if current in visited:
24+
# return current
25+
# visited.add(current)
26+
# current = current.next
27+
# return None

0 commit comments

Comments
 (0)