1. reverse LinkedList
1 class ListNode: 2 def __init__(self,x): 3 self.val=x 4 self.next=None 5 6 class Solution: 7 def reverse(self,head): 8 if(head==None or head.next==None): 9 return head 10 11 cur=head 12 pre=None 13 while(cur): 14 next=cur.next 15 cur.next=pre 16 pre=cur 17 cur=next 18 return pre 19 20 def print_list(self,head): 21 while(head): 22 print(head.val) 23 head=head.next 24 25 26 n1=ListNode(1) 27 n2=ListNode(2) 28 n3=ListNode(3) 29 n4=ListNode(4) 30 n5=ListNode(5) 31 n1.next=n2 32 n2.next=n3 33 n3.next=n4 34 n4.next=n5 35 s=Solution() 36 res=s.reverse(n1) 37 s.print_list(res)
原文:https://www.cnblogs.com/zijidan/p/12496561.html