-
Notifications
You must be signed in to change notification settings - Fork 0
/
141.py
57 lines (40 loc) · 1.03 KB
/
141.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
'''141. Linked List Cycle
Created on 2025-01-01 01:13:43
2025-01-01 01:34:34
@author: MilkTea_shih
'''
#%% Packages
from typing import Optional
#%% Variable
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
#%% Functions
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution_reference:
def hasCycle(self, head: Optional[ListNode]) -> bool:
while head is not None:
if head.val is None:
return True
head.val = None
head = head.next
return False
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
fast = slow = head
while slow and fast is not None and fast.next is not None:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
#%% Main Function
#%% Main
if __name__ == '__main__':
pass
#%%