> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Swap Nodes in Pairs Python Solution with Tests

> Tested Python solution for LeetCode 24 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 24, Medium. Topics: Linked List, Recursion. [View on LeetCode](https://leetcode.com/problems/swap-nodes-in-pairs/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 24   # by problem number
lcpy gen -s swap_nodes_in_pairs   # by problem name
```

## Problem

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)

```
Input: head = [1,2,3,4]
Output: [2,1,4,3]
```

```
Input: head = []
Output: []
```

```
Input: head = [1]
Output: [1]
```

```
Input: head = [1,2,3]
Output: [2,1,3]
```

### Constraints

* The number of nodes in the list is in the range `[0, 100]`.
* `0 <= Node.val <= 100`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/swap_nodes_in_pairs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/swap_nodes_in_pairs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n) - traverse each node once
    # Space: O(1) - constant extra space
    def swap_pairs(self, head: ListNode[int] | None) -> ListNode[int] | None:
        # Create dummy node to simplify edge cases
        dummy = ListNode(0)
        dummy.next = head
        prev = dummy

        # Process pairs while they exist
        while prev.next and prev.next.next:
            # Identify nodes to swap
            first = prev.next
            second = prev.next.next

            # Perform swap: prev -> second -> first -> ...
            prev.next = second
            first.next = second.next
            second.next = first

            # Move prev to end of swapped pair
            prev = first

        return dummy.next
```

## Complexity

| Time                           | Space                       |
| ------------------------------ | --------------------------- |
| O(n) - traverse each node once | O(1) - constant extra space |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
