https://leetcode.com/problems/swap-nodes-in-pairs/
# 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]:
n = 0
cur = head
pre = None
while cur:
first = cur
if not cur.next :
break
second = cur.next
first.next = second.next
second.next = first
if pre :
pre.next = second
else :
head = second
pre = first
if cur.next :
cur = cur.next
else :
cur = None