1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| # https://leetcode.cn/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: ListNode) -> ListNode: p, p.next = self, head while p.next and p.next.next: a, b = p.next, p.next.next p.next, b.next, a.next = b, a, b.next p = a return self.next
|