1.从尾到头打印链表
#这个只是一个输出的数字,并不是一个链表节点
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
arr=[]
while listNode != None:
arr.append(listNode.val)
listNode = listNode.next
return arr[::-1]
2.反转链表,输出新链表的表头
#反转的图解参考链接:https://www.jianshu.com/p/85ef4e64ca5a
#https://blog.csdn.net/xyh269/article/details/70238501
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
pre=None
mid=None
while pHead != None:
mid=pHead.next
pHead.next=pre
pre=pHead
pHead=mid
#pHead.next=pre
return pre