O(n) time code getting a TLE problem


  • 0
    D

    I wrote a code like following which should be run in O(n) time, while I got TLE warning, anyone knows the reason?

    class Solution:
        # @param head, a ListNode
        # @return a list node
        def detectCycle(self, head):
            if not head or not head.next:
                return None
            current = head
            while current.next:
                previous = current
                current = current.next
                if current.next == current:
                    return previous
                previous.next = previous
            return None

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.