classListNode(object):def__init__(self,val=None):
self.val = val
self.next=NonedefmakelinkedList(*args):
temp = ListNode()
head = temp
for x in args:
temp.next= ListNode(x)
temp = temp.nextprint(id(head),id(temp))return head.nextdefprintFromTailToHead(ListNode):
res =[]if ListNode==None:return res
elif ListNode.next==None:return res.append(ListNode.val)else:while ListNode.next:
res.append(ListNode.val)
ListNode = ListNode.next
res.append(ListNode.val)return res[::-1]# a = ListNode(1)# b = ListNode(2)# c = ListNode(3)# d = ListNode(4)# a.next = b# b.next = c# c.next = d
head = makelinkedList(1,2,3,4)print(printFromTailToHead(head))