Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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
Python “中的运行时错误”;“从链表中删除循环”;_Python_Algorithm_Linked List - Fatal编程技术网

Python “中的运行时错误”;“从链表中删除循环”;

Python “中的运行时错误”;“从链表中删除循环”;,python,algorithm,linked-list,Python,Algorithm,Linked List,我正在尝试以下代码挑战: 您将获得一个N节点的链接列表。任务是从链表中删除循环(如果存在) 注意:C是最后一个节点连接到的节点的位置。如果为0,则没有循环 例1: 输入: N = 3 value[] = {1,3,4} C = 2 N = 4 value[] = {1,8,3,4} C = 0 输出:1 说明:在第一个测试用例中 N=3.带有节点的链表 N=3。这里,x=2,哪个 表示最后一个节点与xth连接 链接列表的节点。因此, 存在一个循环 例2: 输入: N = 3 value[]

我正在尝试以下代码挑战:

您将获得一个N节点的链接列表。任务是从链表中删除循环(如果存在)

注意:C是最后一个节点连接到的节点的位置。如果为0,则没有循环

例1: 输入:

N = 3
value[] = {1,3,4}
C = 2
N = 4
value[] = {1,8,3,4}
C = 0
输出:1

说明:在第一个测试用例中 N=3.带有节点的链表 N=3。这里,x=2,哪个 表示最后一个节点与xth连接 链接列表的节点。因此, 存在一个循环

例2: 输入:

N = 3
value[] = {1,3,4}
C = 2
N = 4
value[] = {1,8,3,4}
C = 0
输出:1

说明:N=4和x=0,其中 表示lastNode->next=NULL,因此 链接列表不包含 任何循环

您的任务: 您的任务是完成函数
removeLoop()
。函数的唯一参数是链表的头指针。只需删除列表中的循环(如果存在),而无需断开列表中任何节点的连接。如果代码正确,驾驶员代码将打印1

预期时间复杂度:O(n)

预期辅助空间:O(1)

限制条件:
我得到一个运行时错误。有人能告诉我为什么吗?

有几个问题:

  • 当列表没有循环时,
    x

      while x.next!=temp.next:
    
  • 当列表有一个循环时,第一个循环永远不会退出

  • 当循环包含所有节点时(“尾部”链接返回到第一个节点),则此代码将断开头部节点和第二个节点之间的链接,这显然是错误的。这是一种边界情况,需要单独的解决方案

前两个问题深入到缩进问题。第二个
while
循环应仅在检测到循环时执行。最简单的方法是将其与
return
语句一起移动到检测到循环的
if
中:

def removeLoop(head):
    slow = fast = head
    while fast!=None and fast.next!=None:
        slow = slow.next
        fast = fast.next.next
        if slow==fast:
            if slow == head:  # special case
                # find the "tail" node
                while slow.next != head:
                    slow = slow.next
            else:
                while slow.next != head.next:
                    slow = slow.next
                    head = head.next
            slow.next = None
            return
据我所知,无需返回任何值,因此无需返回
head

示例运行 下面是用一些样板代码完成的代码,以及针对以下问题的运行:

N = 5
value[] = {7,58,36,34,16}
C = 1
因此,这表示具有循环的以下列表:

7 → 58 → 36
↑         ↓
16   ←   34     
removeLoop
功能将删除16和7之间的链接

class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

    def print(self):
        node = self
        for _ in range(10):
            if not node:
                break
            print(node.val, end=" ")
            node = node.next
        print("..." if node else "")

def createLinkedList(values, cycle_start_index):
    # create all nodes
    nodes = [Node(val) for val in values]
    for i, node in enumerate(nodes):
        if i:  # link node from previous
            nodes[i-1].next = node
    if cycle_start_index > 0:  # create the cycle
        nodes[-1].next = nodes[cycle_start_index-1]  # 1-based index
    return nodes[0]

def removeLoop(head):
    slow = fast = head
    while fast!=None and fast.next!=None:
        slow = slow.next
        fast = fast.next.next
        if slow==fast:
            if slow == head:  # special case
                # find the "tail" node
                while slow.next != head:
                    slow = slow.next
            else:
                while slow.next != head.next:
                    slow = slow.next
                    head = head.next
            slow.next = None
            return

# demo
lst = createLinkedList([7, 58, 36, 34, 16], 1)
lst.print()  # 7 58 36 34 16 58 36 34 16 58 ...
removeLoop(lst)
lst.print()  # 7 58 36 34 16 

明白你的逻辑,先生,但是输入失败了-输入:57583634161它正确的输出是:1而你的代码的输出是:0为什么@trincotI现在明白了,当您说输出应该是1时,这与removeLoop函数无关,而是与测试运行程序有关。当输入描述一个完整的循环(将尾部链接回头部)时,我确实看到了一个问题。我已经相应地更新了我的答案。你检查了吗?有什么反馈吗?对不起,我无法回复,先生。是的,代码起作用了。谢谢!