---
title: "LeetCode #24: Swap Nodes in Pairs"
url: https://perrotta.dev/2025/12/leetcode-%2324-swap-nodes-in-pairs/
last_updated: 2026-01-03
---


[LeetCode #24: Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs):

```python
# 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]:
        if not head or not head.next:
            return head

        first = head
        second = head.next
        third = head.next.next

        first.next = self.swapPairs(third)
        second.next = first

        return second
```

