forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmjuu.py
28 lines (23 loc) Β· 884 Bytes
/
pmjuu.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
'''
μκ° λ³΅μ‘λ: O(n)
- `fast`μ `slow` ν¬μΈν°κ° 리μ€νΈλ₯Ό ν λ² μννλ©΄μ μ£Όμ΄μ§ μ°κ²° 리μ€νΈμ κΈΈμ΄μ λΉλ‘νλ μμ
μ μνν©λλ€.
- λ°λΌμ μ΅μ
μ κ²½μ° λͺ¨λ λ
Έλλ₯Ό ν λ²μ© λ°©λ¬Ένκ² λλ―λ‘ O(n)μ
λλ€.
κ³΅κ° λ³΅μ‘λ: O(1)
- μΆκ°μ μΈ μλ£κ΅¬μ‘°λ₯Ό μ¬μ©νμ§ μκ³ , `fast`μ `slow`λΌλ λ κ°μ ν¬μΈν°λ§ μ¬μ©νλ―λ‘ O(1)μ
λλ€.
'''
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False