1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why do i get <__main__.Node object at 0x04E2AA30> instead of the value for rotating...

Discussão em 'Python' iniciado por Stack, Setembro 15, 2025.

  1. Stack

    Stack Membro Participativo

    class Node:
    def __init__(self, dataval=None):
    self.dataval = dataval
    self.nextval = None


    class LinkedList:
    def __init__(self):
    self.headval = None

    def __iter__(self):

    return self.headval

    def printList(self):
    printval = self.headval
    while printval is not None:
    print(printval.dataval)
    printval = printval.nextval

    def addBeginning(self, newdata):
    NewNode = Node(newdata)
    NewNode.nextval = self.headval
    self.headval = NewNode


    def rotateRight(head, k):
    listLength = 1
    listTail = head.headval
    while listTail.nextval is not None:
    listTail = listTail.nextval
    listLength += 1

    offset = abs(k) % listLength

    newTailPos = listLength - offset if k > 0 else offset
    newTail = head.headval

    for i in range(1, newTailPos):
    newTail = newTail.nextval
    print(newTail)

    newHead = newTail.nextval
    newTail.nextval = None
    listTail.nextval = head.headval

    return newHead


    llist = LinkedList()
    llist.headval = Node(1)
    e2 = Node(2)
    e3 = Node(3)
    e4 = Node(4)
    e5 = Node(5)


    llist.headval.nextval = e2
    e2.nextval = e3
    e3.nextval = e4
    e4.nextval = e5

    llist.addBeginning(0)


    rotateRight(llist, 3)


    I am suppose to create a function that will take a linked list and rotate it k positions to the right. I tried debugging the code but I keep getting <__main__.Node object at 0x04E2AA30> as a return value.

    How can I convert that into the type of data I want like a string or an integer?

    Suppose I have a function

    def rotateRight(head, k):
    listTail = head
    while listTail.next is not None:
    #do something


    this code does not work but for some reason

    listTail = head.headval


    works because in my LinkedList class I defined the __ init __ function

    self.headval = None


    If someone can briefly explain or refer me to somewhere that explains this I would appreciate it.

    Continue reading...

Compartilhe esta Página