How to print a linked list from end to end by JZ3
This article shows you how JZ3 prints linked lists from end to end, which is concise and easy to understand, and can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Print the linked list from end to end:
Enter a linked list and return an ArrayList in the order from end to end of the list.
Core code #-*-coding:utf-8-*-# class ListNode:# def _ _ init__ (self, x): # self.val = x # self.next = Noneclass Solution: # returns the sequence of list values from tail to header For example, def printListFromTailToHead (self, listNode): # write code here l = list () while listNode: l.append (listNode.val) listNode = listNode.next l.reverse () return l debug code # enter a linked list and return an ArrayList in the order from end to top of the list. #-*-coding:utf-8-* -''defines the class of a node' 'class ListNode: def _ _ init__ (self, x): # the value pointing to the node self.val = x # points to the next node self.next = None# utility class (incoming node, returns reverse list) class Solution: # returns the sequence of list values from tail to header For example, def printListFromTailToHead (self, listNode): l = [] # determine whether the node exists, and if so, store the value of the node in the list And point to the next node while listNode: l.append (listNode.val) listNode = listNode.next l.reverse () return lif _ _ name__ = ='_ _ main__': # instantiate several new nodes And assign the initial value node1 = ListNode (1) node2 = ListNode (2) node3 = ListNode (3) # to establish the pointing relationship between lists node1.next = node2 node2.next = node3 # example chemical equipment class solution = Solution () # output return value a = solution.printListFromTailToHead (node1) print (a) the above is how JZ3 prints the linked list from end to end. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.