Swap Nodes in Pairs in Python

class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # Create a dummy node to handle edge cases
        dummy = ListNode(0, head)
        prev_node = dummy

        # Traverse the list in pairs
        while prev_node.next and prev_node.next.next:
            # Identify the nodes to be swapped
            first_node = prev_node.next
            second_node = prev_node.next.next

            # Perform the swap
            prev_node.next = second_node
            first_node.next = second_node.next
            second_node.next = first_node

            # Move to the next pair
            prev_node = first_node

        # Return the new head of the list
        return dummy.next