人生苦短,我用python,然而还是不经意的暴露出很多C++的编码习惯
题目很简单,用一个哈希表就OK了
但是这运行效率就比较感人了,和C++不是一个档次的
# 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
"""
hashlist=[]
while(head!=None):
if head in hashlist:
return True
else:
hashlist.append(head)
head=head.next
return False