Swap Nodes in Pairs

Try to solve the Swap Nodes in Pairs problem.

Statement

Given a singly linked list, swap every two adjacent nodes of the linked list. After the swap, return the head of the linked list.

Note: Solve the problem without modifying the values in the list’s nodes. In other words, only the nodes themselves can be changed.

Constraints:

  • The number of nodes in the list is in the range [0,100][0, 100].
  • 0≤0 \leq Node.value ≤100\leq 100

Examples

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy