Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the nodes.

30:00

Swap Nodes in Pairs
medium
Topics
Companies

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the nodes.

Example 1:
Input: [1,2,3,4]
Output: [2,1,4,3]
Constraints:
  • The number of nodes is in the range [0,100][0, 100].

  • 0Node.val1000 \leq \text{Node.val} \leq 100

Input
arr =[1,2,3,4]

Start swapping

1pos 02pos 13pos 24pos 3NULL
Current pair: 1
Pairs swapped: 0
Total nodes: 4
Variables
No variables to display
DepthFunction Call
Stack empty
0/3