给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
直接使用递归
class Solution:
def swapPairs(self, head):
if head == None:
return None
if head.next == None:
return head
nextNode = head.next
nextNode.next = self.swapPairs(nextNode.next)
head.next = nextNode.next
nextNode.next = head
return nextNode