Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 求算法的时间复杂度_Algorithm_Data Structures_Linked List_Time Complexity - Fatal编程技术网

Algorithm 求算法的时间复杂度

Algorithm 求算法的时间复杂度,algorithm,data-structures,linked-list,time-complexity,Algorithm,Data Structures,Linked List,Time Complexity,我需要编写一个算法,在链表中查找循环的长度并返回(如果循环存在)以及循环中的一个元素。 如果列表中没有循环,则返回None。 这是我的密码: def find_circle(self): slower = self.head faster = self.head while (slower is not None and faster is not None): if faster.next is not None: faster

我需要编写一个算法,在链表中查找循环的长度并返回(如果循环存在)以及循环中的一个元素。 如果列表中没有循环,则返回None。 这是我的密码:

def find_circle(self):
    slower = self.head
    faster = self.head
    while (slower is not None and faster is not None):
        if faster.next is not None:
            faster = faster.next.next
        slower = slower.next
        if slower == faster:
            counter = 1
            slower = slower.next
            while slower != faster:
                slower = slower.next
                counter += 1
            return counter, slower.data
    return None
代码的时间复杂度必须为O(n),额外的空间复杂度必须为O(1)。
此代码符合时间和空间复杂度吗?

是的,上述代码的时间复杂度为
O(n)
,空间复杂度为
O(1)
,因为您没有存储整个列表

def find_circle(self):
    // Space O(1)
    slower = self.head
    faster = self.head

    // Looping through only once, hence time complexity is O(n)
    while (slower is not None and faster is not None):
        if faster.next is not None:
            faster = faster.next.next
        slower = slower.next
        if slower == faster:
            counter = 1
            slower = slower.next
            while slower != faster:
                slower = slower.next
                counter += 1
            return counter, slower.data
    return None

是的,上面代码的时间复杂度是
O(n)
,空间复杂度是
O(1)
,因为您没有存储整个列表

def find_circle(self):
    // Space O(1)
    slower = self.head
    faster = self.head

    // Looping through only once, hence time complexity is O(n)
    while (slower is not None and faster is not None):
        if faster.next is not None:
            faster = faster.next.next
        slower = slower.next
        if slower == faster:
            counter = 1
            slower = slower.next
            while slower != faster:
                slower = slower.next
                counter += 1
            return counter, slower.data
    return None

这需要一个更微妙的论点来解释为什么它在时间O(n)内运行。例如,如果列表中确实包含一个循环,那么您如何知道这两个指针将以O(n)步而不是O(n^2)步相互碰撞?在一个循环中,两个指针可能会出现n^2对位置,你需要解释为什么在两个指针相遇之前,你不能把它们全部看完。需要一个更细致的论证来解释为什么这会在时间O(n)内运行。例如,如果列表中确实包含一个循环,那么您如何知道这两个指针将以O(n)步而不是O(n^2)步相互碰撞?在一个循环中,有n^2个可能的位置对,两个指针可以使用,并且您需要解释为什么在两个指针相遇之前,您不会遍历所有这些位置。