題目描述
輸入一個鏈表,反轉(zhuǎn)鏈表后震束,輸出新鏈表的表頭怜庸。
Python
class Solution:
? ? # 返回ListNode
? ? def ReverseList(self, pHead):
? ? ? ? # write code here
? ? ? ? if pHead is None:
? ? ? ? ? ? return None
? ? ? ? pre = None
? ? ? ? while pHead.next:
? ? ? ? ? ? tmp = pHead.next
? ? ? ? ? ? pHead.next = pre
? ? ? ? ? ? pre = pHead
? ? ? ? ? ? pHead = tmp
? ? ? ? pHead.next = pre
? ? ? ? return pHead