您的位置:首页 > 编程语言 > Python开发

【LeetCode with Python】 Linked List Cycle

2008-12-07 13:29 435 查看
博客域名:http://www.xnerv.wang

原题页面:https://oj.leetcode.com/problems/linked-list-cycle/

题目类型:

难度评价:★

本文地址:/article/1377542.html

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

Follow up:

Can you solve it without using extra space?

确定链表是否有环。记得好像是编程之美上的题目?

class Solution:
    # @param head, a ListNode
    # @return a boolean
    def hasCycle(self, head):
        if None == head:
            return False
        fast = slow = head
        while True:
            fast = fast.next
            if None == fast:
                return False
            fast = fast.next
            if None == fast:
                return False
            slow = slow.next
            if fast == slow:
                return True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: