Easy solution in Python3

programming
Tags: #<Tag:0x00007f18232bb410>

#1

def solve(self, A, B):

    n = self.getCount(A)
    reqNode = int((n / 2 + 1) - B)
    
    if reqNode<=0:
        return -1
    else:
        current = A
        count = 1
        while current:
            if count == reqNode:
                return current.val
            count += 1
            current = current.next
    return A
    
def getCount(self, h):
    count = 0
    current = h
    while current:
        count += 1
        current = current.next
        
    return count