Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Swap Nodes in Pairs

Easy
linked-listrecursion

Swap every two adjacent nodes of the list and return the new head. Swap the nodes themselves, not just their values.

Input: one line of space-separated integers — the list values (may be empty). Output: the resulting list's values, space-separated.

Example 1
Input:
1 2 3 4
Output:
2 1 4 3
Example 2
Input:
1
Output:
1
Example 3
Input:
1 2 3
Output:
2 1 3

Constraints

  • 0 ≤ list length ≤ 100
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.