学习目标:
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表
学习内容:

解法一: 思路:使用栈的特性,栈是先进后出的。实现原理就是把链表节点一个个入栈,当全部入栈完之后再一个个出栈,出栈的时候在把出栈的结点串成一个新的链表 
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
stack = list()
if head == None:
return None
while head.next is not None:
stack.append(head)
head = head.next
stack.append(head)
if len(stack) == 0:
return None
node = stack.pop()
result_head = node
while len(stack) > 0:
tmp_node = stack.pop()
node.next =tmp_node
node = node.next
node.next = None
return result_head

|