-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path141.py
More file actions
34 lines (27 loc) · 928 Bytes
/
141.py
File metadata and controls
34 lines (27 loc) · 928 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
33
34
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# 第一個方法就是用一個list來存走過的路有哪些,然後再看看有沒有走過
# 第二個方法就是把每個走過的點都變成最大的val(float inf),到時候就知道這個點有沒有走過了
class Solution:
# Solution 1
def hasCycle(self, head: Optional[ListNode]) -> bool:
ls = []
while head:
if head.next in ls:
return True
ls.append(head.next)
head = head.next
return False
# Solution 2
def hasCycle(self, head: Optional[ListNode]) -> bool:
M = float('inf')
temp = head
while temp:
if temp.val == M:
return True
temp.val = M
temp = temp.next
return False