# Swap Nodes in Pairs - Daily JS (Day 20)

Problem Statement
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

Examples
Example 1:

``````Input: head = [1,2,3,4]
Output: [2,1,4,3]
``````

Example 2:

``````Input: head = []
Output: []
``````

Example 3:

``````Input: head = 
Output: 
``````

Constraints:

``````The number of nodes in the list is in the range [0, 100].
0 <= Node.val <= 100
``````

Solution

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
const swapPairs = (n) => {
if (n === null) {
return null
}
if (n.next === null) {
return n
}
let n1 = n.next
let n2 = n.next.next
n1.next = n
n.next = swapPairs(n2)
return n1
};
``````

LeetCode issue:
https://leetcode.com/problems/swap-nodes-in-pairs/

How to implement a Linked List?

I hope you are enjoying the series, if yes then don't forget to press ❤️ and Follow. You can also bookmark it for later use. If you have any queries or suggestions don't hesitate to drop them.
Thanks will see you in next post. 