This commit is contained in:
parent
b96b438b8a
commit
74ea899d2c
1 changed files with 23 additions and 0 deletions
23
easy/linked_list_cycle.py
Normal file
23
easy/linked_list_cycle.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# https://leetcode.com/problems/linked-list-cycle
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class ListNode:
|
||||||
|
val: int
|
||||||
|
next: Optional["ListNode"]
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def hasCycle(self, head: Optional[ListNode]) -> bool:
|
||||||
|
first: Optional[ListNode] = head
|
||||||
|
second: Optional[ListNode] = head
|
||||||
|
|
||||||
|
while first is not None and \
|
||||||
|
first.next is not None and \
|
||||||
|
second is not None:
|
||||||
|
|
||||||
|
first = first.next.next
|
||||||
|
second = second.next
|
||||||
|
if first == second:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue