classSolution:defReverseList(self , head: ListNode)-> ListNode:
pre, cur =None, head
while cur isnotNone:
k = cur.next
cur.next= pre
pre = cur
cur = k
return pre
classSolution(object):defswapPairs(self, head):
pre = self
pre.next= head # 这里pre是solution,next相当于他的一个属性while pre.nextand pre.next.next:# pre来回变化,从solution类型转换为ListNode类型
a = pre.next
b = pre.next.next
pre.next= b
a.next= b.next
b.next= a
pre = a
return self.next
反转链表:思路:记录当前指针和前一个指针,进行指针的反向,直到当前指针为空停止,返回pre即为最后一个结点,也就是遍历完的头。class Solution: def ReverseList(self , head: ListNode) -> ListNode: pre, cur = None, head while cur is not None: k = cur.next cur.next = pre .