• + 0 comments

    PYTHON using list

    def getNode(llist, positionFromTail): if not llist: return None

    E = []
    temp =llist
    while temp != None:
        E.append(temp.data)
        temp = temp.next
    
    #   we use positionfromTail -1 bcz we calculate the postion from 1 
        position = len(E) -1 -positionFromTail
    return E[position]
    

    PYTHON using LinkedList

    def getNode(llist, positionFromTail): if not llist: return None

    temp = llist.next
    count = 0
    
    while temp != None:
        count +=1
        temp = temp.next
    
    while count > positionFromTail:
        count -= 1
        llist = llist.next
    
    return llist.data
    

    to N but when indexing it starts form 0 so for that we have to do -1 position = len(E) -1 - positionFromTail return E[position]