LC 0024 [M] Swap Nodes in Pairs - ALawliet/algorithms GitHub Wiki

# 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]:
        dummy = ListNode(0, head)
        prev, cur = dummy, head
        
        while cur and cur.next:
            # save ptrs
            nxtPair = cur.next.next
            second = cur.next
            
            # reverse this pair
            second.next = cur
            cur.next = nxtPair
            prev.next = second
            
            # update ptrs
            prev = cur
            cur = nxtPair
        
        return dummy.next