# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: defswapPairs(self, head: ListNode) -> ListNode: ans = cur = ListNode(0) ans.next = head while cur.next and cur.next.next: a = cur.next b = a.next a.next, b.next, cur.next = b.next, a, b cur = cur.next.next return ans.next