-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path141.linked-list-cycle.py
More file actions
27 lines (24 loc) · 1013 Bytes
/
141.linked-list-cycle.py
File metadata and controls
27 lines (24 loc) · 1013 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
#
# @lc app=leetcode id=141 lang=python3
#
# [141] Linked List Cycle
#
# @lc code=start
# 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:
if not head: # Kiểm tra nếu danh sách rỗng
return False # Trả về False nếu danh sách rỗng
slow = head # Khởi tạo con trỏ chậm
fast = head # Khởi tạo con trỏ nhanh
while fast and fast.next: # Lặp cho đến khi con trỏ nhanh hoặc con trỏ tiếp theo là None
slow = slow.next # Di chuyển con trỏ chậm một bước
fast = fast.next.next # Di chuyển con trỏ nhanh hai bước
if slow == fast: # Nếu hai con trỏ gặp nhau
return True # Trả về True nếu có vòng lặp
return False # Trả về False nếu không có vòng lặp
# @lc code=end