# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = NoneclassSolution(object):defreverseList(self, head):"""
:type head: ListNode
:rtype: ListNode
笨辦法
1. 遍歷鏈表,生成一個數組;
2. 根據數組,倒序創建鏈表;
聰明辦法
改指針方向
"""
cur = head
pre =Nonewhile cur isnotNone:next= cur.next
cur.next= pre
pre = cur
cur =nextreturn pre