# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
# write code here
a0 = ListNode(0)
# x0为指针
a0.next, x0 = head, a0
while x0.next and x0.next.next:
a1, a2 = x0.next, x0.next.next
x0.next = a2
a1.next = a2.next
a2.next = a1
x0 = a1
return a0.next

京公网安备 11010502036488号