题目要求:
思路:
思路1:使用列表,模拟入栈出栈操作进行重构列表
代码如下:
思路1代码:
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def ReverseList(self , head: ListNode) -> ListNode:
# write code here
stack = []
while head:
stack.append(head)
head = head.next
if stack:
pre_head = now_node = stack.pop()
while stack:
now_node.next = stack.pop()
now_node = now_node.next
now_node.next = None
return pre_head
else:
return None