递推法:
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
p=ListNode(0)
p.next=head
t=p
while t.next and t.next.next:
i=t.next
j=i.next
i.next=j.next
j.next=i
t.next=j
t=i
return p.next
递归:
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
if not(head and head.next):
return head
t=head.next
head.next=self.swapPairs(t.next)
t.next=head
return t