【LeetCode with Python】 Linked List Cycle

来源:互联网 发布:three.min.js下载 编辑:程序博客网 时间:2024/06/05 09:21
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/linked-list-cycle/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/3465710

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