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