Skip to main content
LeetCode 24, Medium. Topics: Linked List, Recursion. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

Grind, NeetCode All, AlgoMaster 75.
Last modified on August 25, 2026