码迷,mamicode.com
首页 > 其他好文 > 详细

141. Linked List Cycle

时间:2017-01-17 15:23:42      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:can   follow   code   nbsp   return   false   min   space   style   

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

 

# 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
        """
        if head==None:
            return False
        slow=head
        fast=head
        while fast.next!=None and fast.next.next!=None:
            slow=slow.next
            fast=fast.next.next
            if slow is fast: return True
        return False

 

141. Linked List Cycle

标签:can   follow   code   nbsp   return   false   min   space   style   

原文地址:http://www.cnblogs.com/rocksolid/p/6293008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!