给定一个链表,判断链表中是否有环
示例代码:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
plow = head
pfast = head
if head == None or head.next == None:
return False
while pfast != None and pfast.next != None:
pfast = pfast.next.next
plow = plow.next
if pfast == plow:
return True
return False