环形链表python3(leetcode141)

#141. 环形链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        #判断是否存在环,是leetcode142的前提条件
        slow, fast = head, head
        while(fast and fast.next):
            slow, fast = slow.next, fast.next.next
            if(slow == fast):
                return True
        else:
            return False

猜你喜欢

转载自blog.csdn.net/ziqingnian/article/details/121923360
今日推荐