腾讯24-环形链表

腾讯24-环形链表leetcode141

给定一个链表,判断链表中是否有环。
leetcode或者剑指 offer最让人难忘的题目,快慢指针,比较好记住

# 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:
        if head is None or head.next is None :
            return False
        else:
            fast,slow=head,head
            while(fast is not None and fast.next is not None ):
                fast=fast.next.next
                slow=slow.next
                if fast==slow:
                    return True
            return False
发布了93 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zlb872551601/article/details/103644581