# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
# 思路是递归
if not head or not head.next:
return head
temp=head.next
head.next=self.swapPairs(head.next.next)
temp.next=head
return temp
思路是:
- 递归