您的位置:首页 > 其它

141. Linked List Cycle

2017-09-05 21:20 429 查看
Given a linked list, determine if it has a cycle in it.

Follow up:

Can you solve it without using extra space?

判断链表是否有环的技巧:一个走两步,一个走一步,查看是否汇合

class Solution(object):
def hasCycle(self, head):
p = head
q = head
has_cycle = False
try:
while(p and p.next):
p = p.next
q = q.next.next
if p == q:
has_cycle = True
break
except AttributeError:
pass
return has_cycle
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: