class ListNode:
def __init__(self,x):
self.val=x
self.next=None
'''
递归操作,先将从第一个点开始翻转转换从下一个节点开始翻转
直至只剩一个节点
时间消耗O(n),空间消耗O(1)
'''
def reverse_linkedlist4(head):
if head is None or head.next is None:
return head
else:
newhead=reverse_linkedlist4(head.next)
head.next.next=head
head.next=None
return newhead
单链表反转
最新推荐文章于 2021-04-23 20:27:36 发布