-
Notifications
You must be signed in to change notification settings - Fork 4
/
linked-list-cycle-ii.py
64 lines (55 loc) · 1.87 KB
/
linked-list-cycle-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 142. Linked List Cycle II
# 🟠 Medium
#
# https://leetcode.com/problems/linked-list-cycle-ii/
#
# Tags: Hash Table - Linked List - Two Pointers
from typing import Optional
from utils.linked_list import ListNode
# Use the fast and slow pointer cycle detection algorithm.
# https://en.wikipedia.org/wiki/Cycle_detection
#
# Time complexity: O(n) - The complexity is linear over the number of
# nodes in the list.
# Space complexity: O(1) - We use constant extra space.
#
# Runtime 50 ms Beats 72.17%
# Memory 17.2 MB Beats 90.55%
class Floyd:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
if slow == fast:
break
# If the loop exits and the pointers do not point to the same
# node, there is no cycle.
else:
return None
fast = head
while fast != slow:
slow, fast = slow.next, fast.next
return fast
# If the list nodes are hashable, or if they have unique values that we
# can hash, we can use a set, or a hashmap of value: node, to add
# nodes to, if we see a node that is already in the hash set, we can
# return it because that is the start of the cycle.
#
# Time complexity: O(n) - Linear time if we consider hashing O(1).
# Space complexity: O(n) - The set can grow to size n.
#
# Runtime 55 ms Beats 52.57%
# Memory 17.9 MB Beats 21.35%
class Set:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
current = head
seen = {current}
while current.next:
current = current.next
if current in seen:
return current
seen.add(current)
return None
print(f"\033[93m» This file does not have any tests\033[0m")