一种在python中以倒序/倒序打印链表的方法 [英] A method to print linked list in reverse/backward order in python

查看:73
本文介绍了一种在python中以倒序/倒序打印链表的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个名为LList的类,并在该类中定义了几个方法.我在编写一种以反向顺序打印链表的方法时遇到问题.

I'm writing a class called LList and in the class I defined a couple of methods. I am having issue with writing a method to print the linked list in backward order.

class Node(object):
    def __init__(self, data=None, nextNode=None):
        self.data = data
        self.nextNode = nextNode

class LList(object):

    def __init__(self, head=None):
        self.head = head
        self.size = 0

    def insert(self, node):

        if not self.head:
            self.head = node
            self.size += 1
        else:
            # set new nodes pointer to old head
            node.nextNode = self.head
            # reset head to new node
            self.head = node
            self.size +=1

    def getSize(self):
        return self.size

    def printLL(self):
        mynode = self.head
        c = 0
        while mynode:
            c += 1
            print(mynode.data, c)
            mynode = mynode.nextNode

#a method for class that prints list backwards.       
    def reverse(self):


#main program

    MyList = LList()
    MyList.insert(Node("NJ"))
    MyList.insert(Node("NR"))
    MyList.insert(Node("OH"))

    # Use my print method
    MyList.printLL()
    #print in reverse
    MyList.reverse()

推荐答案

class Node(object):

          def __init__(self, data=None, next_node=None):
              self.data = data
              self.next = next_node

       def ReversePrint(head):
           if head == None:
              return
           else:
             ReversePrint(head.next)
             print(head.data)

让我们以链接列表为例:1-> 2-> 3遍历代码,为ReversePrint函数提供了到链接列表的开头的链接,该链接为1.我们看到head不等于null,因此我们移至代码的第二行,这是第一个递归调用到head.next,即2.再次,我们看到这个新的"head"不等于null,因此我们进行了第二次递归调用,这次传递了包含3的节点.我们看到这个新的"head"是不等于null,因此我们对head.next进行第三次递归调用,在这种情况下为null.所以我们返回,因为我们击中了基本情况.现在返回第二个递归调用,然后移至递归调用之后的代码行,即print语句.我们打印3.由于此递归调用现在已完成,所以我们移回到第一个递归调用,并打印2.最后,我们返回到初始函数调用,并打印1.因此,成功反向打印了链接列表命令.

Let's take an example Linked List: 1->2->3 Walking through the code, the function ReversePrint is given a link to the head of our Linked List, which is 1. We see that head is not equal to null so we move to the second line of the code, which is the first recursive call to head.next, which is 2. Again we see that this new "head" is not equal to null, so we do a second recursive call, this time passing the node that contains 3. We see that this new "head" is not equal to null, so we do a third recursive call on head.next, which is null in this case. So we return, since we hit our base case. We are now back in our second recursive call, and we move to the line of code that comes after the recursive call, which is the print statement. We print 3. Since this recursive call is now complete, we move back to the first recursive call, and print 2. Finally, we are back in our initial function call, and we print 1. Therefore, successfully printing our Linked List in reverse order.

这篇关于一种在python中以倒序/倒序打印链表的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆