1

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.

However, this doesn't even run - it gives me a 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? 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

1 Answer 1

2

middleNode is a method of Solution, but you never instanced Solution. The entire last if statement is inside of your 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.

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. 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. 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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! What if I wanted to call the middleNode function in a different function in class Solution?
Regarding why not just use a list- just practicing understanding linked lists in python at the moment :) Thanks for the clarifications!
one last question - suppose I wanted to call middlenode while in my original setup in class Solution - would I be able to do if I created a different function inside Solution?
It seems you want to create a proxy script to manage your linked lists. This isn't good OOP. Imagine if you had a class representing "Person", but everything a person could do was managed by some other class. That would be the equivalent of creating a handicapped person. The less dependencies your scripts have the easier it is to modify those scripts without breaking everything else. If you haven't already, go study the data at the following link. realpython.com/linked-lists-python/#understanding-linked-lists. That will give you a good starter-kit for creating and managing linked lists
In continuation of my last comment. An alternate way to understand what you are asking me is to assume you are asking if you can call middleNode from inside of Solution if middleNode was a method of Solution. The answer is yes. self.middleNode()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.