Python:NameError:未定义全局名称 'sortList'(递归期间)

标签 python recursion

在递归行 (l1 = sortList(head)) 中,我得到 NameError: global name 'sortList' is not defined。 谁能指出我哪里做错了?

class Solution:
    # @param head, a ListNode
    # @return a ListNode

    def sortList(self, head):
        if head == None or head.next == None:
            return head

        slow = head
        fast = head

        while fast != None and fast.next != None:
            slow = slow.next
            fast = fast.next.next
        fast = slow
        slow = slow.next
        fast.next = None
        l1 = sortList(head)
        l2 = sortList(slow)
        l = mergeTwoLists(l1, l2)
        return l

最佳答案

sortListSolution 的一个方法,不是独立存在的。 使用:

self.sortList(head)

它会起作用。

关于Python:NameError:未定义全局名称 'sortList'(递归期间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24291941/

相关文章:

python - Flask-Testing 模块没有这样的测试方法报错

javascript - 基于 promise 的递归函数中的问题

algorithm - 树递归斐波那契算法需要线性空间?

C递归返回2个值

c - C 中的数组排序

python - 使用非标准化数据在 Scipy 中进行 Kolmogorov-Smirnov 测试

python - 按字母顺序对二维列表进行排序?

python - 将结构从 C++ 传递到 Python

java - 使用递归(java)查找给定数字是否是给定集合(允许重复)的总和

python - 简单逻辑错误(组织列表)