classSolution:defswapPairs(self,head: ListNode) -> ListNode: Head =ListNode(0) Head.next = headifnot head ornot head.next:return head pre, i ,j = Head, head, head.nextwhile i and j:#交换 i.next = j.next j.next = i pre.next = j#更新 pre = i i = i.nextif i:#若i非空才有j j = i.nextreturn Head.next