1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Node(): def __init__(slef,data): self.next=None self.data=data
# 方法一 def reverse(head): if head.next is None: return head
next=head.next head.next=None current_next=revers(next) next.next=head return current_next
# 方法二 def swap_reverse(head): cur,prev=head,None while cur: cur.next,prev,cur=prev,cur,cur.next return prev
|