环形链表python3(leetcode141)

#141. 环形链表

环形链表python3(leetcode141)

 

# 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

上一篇:左偏树


下一篇:2021-10-10