24. Swap Nodes in Pairs
Description
Given alinked list, swap every two adjacent nodes and return its head. You must solve the problem withoutmodifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)
Example 1:
1 | Input: head = [1,2,3,4] |
Explanation:

Example 2:
1 | Input: head = [] |
Example 3:
1 | Input: head = [1] |
Example 4:
1 | Input: head = [1,2,3] |
Constraints:
- The number of nodes in thelistis in the range
[0, 100]
. 0 <= Node.val <= 100
Hints/Notes
- 2025/03/09 Q1
- linked list
- 0x3F’s solution
Solution
Language: C++
1 | /** |