# class ListNode:# def __init__(self, x):# self.val = x# self.next = NoneclassSolution:defreversePrint(self, head: ListNode)-> List[int]:
stack =[]
p = head
while p !=None:
stack.append(p.val)
p = p.nextreturn stack[::-1]
classNode():def__init__(self,data):
self.data = data
self.next=NoneclassList():def__init__(self):
self._head =None# 尾插法插入元素defappend(self, data):# 在单链表后插入元素,需要一个指针遍历到最后一个结点
p = self._head
n = Node(data)if self._head ==None:
self._head = n
else:while p.next!=None:
p = p.next
p.next= n
defreverslist(self):
pre =None
cur = self._head
while cur:
temp = cur.next
cur.next= pre
pre = cur
cur = temp
p = pre
while p !=None:# p.next != Noneprint(p.data, end=' ')
p = p.next
l1 = List()
data =int(input("请输入链表l1中的值,输入-1结束:"))while data!=-1:
l1.append(data)
data =int(input("请输入链表l1中的值,输入-1结束:"))
l1.reverslist()