Title Description
Enter a list that returns a ArrayList in the order of the list values from the end of the header.
Title Address
https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=11156&tPage=1&rp=2 &ru=%2fta%2fcoding-interviews&qru=%2fta%2fcoding-interviews%2fquestion-ranking
Ideas
Using the Python library function, create a new list, insert every time with insert to the front, or use append to use reverse.
#-*-coding:utf-8-*-classListNode:def __init__(self, x): Self.val=x Self.next=None#unidirectional chain table node1:1->2->3Node1 = ListNode (1) Node2= ListNode (2) Node3= ListNode (3) Node4= ListNode (4) Node1.next=Node2node2.next=Node3node3.next=Node4classSolution:defPrintlistfromtailtohead (Self, listnode):#Method 1: Use the Insert function #c = [] #While ListNode: #C.insert (0,listnode.val) #ListNode = Listnode.next #return C #Method 2: Use Append, and finally reversec = [] whilelistnode:c.append (listnode.val) ListNode=listnode.next c.reverse ()returnCif __name__=='__main__': Run=solution () result=Run.printlistfromtailtohead (Node1)Print(Result)
The sword refers to offer 3. Print linked list from tail to head (linked list)