简体   繁体   English

为什么我在 python 中写的这个基本链表方法不能做一个基本的测试?

[英]Why can't I do a basic test on this basic linkedlist method I wrote in python?

Code below.代码如下。 The idea is to test if I'm printing the middle node of the linked list - and if the list has an even number of nodes, to return the first of the 2 middle nodes.这个想法是测试我是否正在打印链表的中间节点 - 如果列表有偶数个节点,则返回 2 个中间节点中的第一个。

However, this doesn't even run - it gives me a TypeError: middleNode() missing 1 required positional argument: 'head'然而,这甚至没有运行——它给了我一个 TypeError: middleNode() missing 1 required positional argument: 'head'

I'm giving it A as the head, and it's defined as a ListNode...so what am I missing?我把它作为头部,它被定义为一个 ListNode ......所以我错过了什么? Why isn't this working as intended?为什么这没有按预期工作?

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

class Solution:
    def middleNode(self, head):
        slow = fast = head
        if not fast.next:
            return fast
        if not fast.next.next:
            return fast
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        return slow



    if __name__ == "__main__":
        A = ListNode(1)
        B = ListNode(2)
        C = ListNode(3)
        A.next = B
        B.next = C
        C.next = ListNode(4)
        C.next.next = ListNode(5)
        C.next.next.next = ListNode(6)

        print(A.next.val)
        print(middleNode(A).val) #This is giving me an error

middleNode is a method of Solution, but you never instanced Solution. middleNode是一种解决方案的方法,但您从未实例化解决方案。 The entire last if statement is inside of your class.整个最后一个 if 语句在您的 class 内。 It needs to be moved to the left margin.它需要移动到左边距。 Those fixes will solve your problem, but you have other issues with being too verbose and spreading your code too thin.这些修复将解决您的问题,但您还有其他问题,即过于冗长和将代码传播得太细。 middleNode should be a property of ListNode , so you have access to it wherever you drag a ListNode . middleNode应该是ListNode的一个属性,因此您可以在任何拖动ListNode的地方访问它。

The problem with your version of middleNode regards it's over-zealousness to determine what is not true, but then proceeded to check what is true.您的middleNode版本的问题在于它过于热心地确定什么是不真实的,但随后继续检查什么是真实的。 All you have to do is automatically assume it isn't true and then check if it is.你所要做的就是自动假设它不是真的,然后检查它是否是真的。 If nothing was true then your assumption get's passed on.如果没有什么是真的,那么你的假设就会被传递。 Otherwise, whatever the truth is will be processed.否则,无论真相如何,都会被处理。 You also missed an "Aha,": that being: if there is a .next.next then there is definitely also a .next .你也错过了一个“啊哈”:那就是:如果有一个.next.next那么肯定还有一个.next By checking the former, the latter is implied.通过检查前者,后者是隐含的。

class ListNode:
    def __init__(self, val = 0, next = None):
        self.val = val
        self.next = next
  
    #Whereas this is a shorter way to write what you wrote
    #I'm not convinced that this actually does what you want it to
    #I'm pretty sure you will always get 3 nodes from the end
    #Or in code terms the head of .next.next.next
    #It only seems to work because you only count to 6
    @property
    def middleNode(self):
        mid = self
        head = self
        while head.next.next:
            mid = mid.next
            head = head.next.next
            
        return mid.val


if __name__ == "__main__":
    A = ListNode(1)
    B = ListNode(2)
    C = ListNode(3)
    A.next = B
    B.next = C
    C.next = ListNode(4)
    C.next.next = ListNode(5)
    C.next.next.next = ListNode(6)

    print(A.next.val)
    print(A.middleNode) 

This cleans up your script, but I have to ask: Why not just use a list?这会清理您的脚本,但我不得不问:为什么不只使用列表? Something like:就像是:

nodes = [10,30,70,90,100,150,180]
print(nodes[int(len(nodes)/2)]) #virtually a middleNode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能安装greenlet(只是一个基本的python包)? - Why can't I install greenlet (just a basic python package)? Python Selenium - 我该如何处理这个基本代码? - Python Selenium - How can I do work this basic code? 我正在尝试在 python 中进行基本的颜色转换,但是我似乎无法克服以下错误 - I am trying to do a basic colour conversion in python however I can't seem to get past the below error 我在python中写了一个文件下载器,如果出现错误404则不能跳过 - I wrote a file downloader in python,can`t skip if Error 404 从python中的包中导入模块,这样的基本问题我想我什至找不到答案 - importing modules from packages in python, such a basic issue I guess I can't even find the answer anyhwere 如何在Python中使用请求创建基本的REST帖子? - How do I do a basic REST Post with Requests in Python? Python ioapiTools模块无法执行基本数学运算 - Python ioapiTools module can't do basic math operations 为什么我不能在 python 中覆盖这个方法? - Why can't I override this method in python? 为什么我的基本Flask代码出现404错误? - Why do I get 404 error with basic Flask code? 为什么我的基本 python UDP 客户端出现 ConnectionResetError? - Why am I getting a ConnectionResetError for my basic python UDP client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM