Back to DSA

Swap Nodes in Pairs

medium
Acceptance: 52%
Linked List

Given a linked list, swap every pair of adjacent nodes. For example, the first node swaps with the second, the third with the fourth, and so on. If the list has an odd number of nodes, the last node remains in place. You may not modify node values — only restructure the links.

Examples

Example 1:
Input:head = [5,10,15,20]
Output:[10,5,20,15]
Example 2:
Input:head = [3]
Output:[3]

Hints

00:00
1234567