from typing import List
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
cur = head
while cur and cur.next:
cur.val, cur.next.val = cur.next.val, cur.val
cur = cur.next.next
return head
def swapPairsRepeatation(self, head:ListNode) -> ListNode:
root = prev = ListNode(None)
prev.next = head
while head and head.next:
b = head.next
head.next = b.next
b.next = head
prev.next = b
head = head.next
prev = prev.next.next
return root.next
def swapPairsRecur(self, head:ListNode) -> ListNode:
if head and head.next:
p = head.next
head.next = self.swapPairsRecur(p.next)
p.next = head
return p
return head