-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
32 lines (28 loc) · 888 Bytes
/
Copy pathSolution.py
File metadata and controls
32 lines (28 loc) · 888 Bytes
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
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
listNodeSet = set()
while head is not None:
if head in listNodeSet:
return head
listNodeSet.add(head)
head = head.next
return None
def detectCycle(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return None
slow = head
fast = head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
if slow == fast:
meetNode = slow
while head != meetNode:
head = head.next
meetNode = meetNode.next
return head
return None