# JavaScript Program For Reversing A Linked List In Groups Of Given Size

A linked list is a linear data structure that consists of interconnected nodes. Reversing a linked list means changing the order of all its elements. Reversing a linked list in groups of a given size means, we are given a number and we will reverse the first given number of elements, and then for the next set we will reverse the elements. We will see the proper code with implementation.

Examples

Given linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
Given number: 3
Output: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 8 -> 7 -> null


Explanation − In the given linked list we have to make the group of 3-3 elements which leads to three groups: 1, 2, 3 groups one and we will reverse it as 3, 2, and 1. Similarly, for the second groups 4, 5, and 6 it will be reversed as 6, 5, and 4. In the end, the last group is not of the exact given number size so we will just take the number of elements that are left. So for 7 and 8, we will reverse them as 8 and 7.

Let’s see the different approaches to solving this problem −

## Example: Iterative Approach

// class to create the structure of the nodes
class Node{
constructor(data){
this.value = data;
this.next = null;
}
}
// function to print the linked list
console.log("The given linked list is empty");
} else {
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
}
console.log(ans)
}
return tail.next = new Node(data);
}
// function to remove the duplicate numbers
if (!head || number == 1){
}
var temp = new Node();
temp.value = -1;
var prev = temp;
var cur = temp;
var next = temp;
// Calculating the length of linked list
var len = 0;
while (cur) {
len++;
cur = cur.next;
}
// Iterating till next is not NULL
while (next) {
cur = prev.next;
next = cur.next;
var toL = len > number ? number : len - 1;
for (var i = 1; i < toL; i++) {
cur.next = next.next;
next.next = prev.next;
prev.next = next;
next = cur.next;
}
prev = cur;
len -= number;
}
return temp.next;
}
// given number
var number = 3
console.log("The given linked list is: ")
// calling function to reverse elements in group
console.log("The Linked list after reversing elements in groups of 3 is: ")


## Example: Recursive Approach

In this approach, we are going to use the concept of recursion, let us see the code −

// class to create the structure of the nodes
class Node{
constructor(data){
this.value = data;
this.next = null;
}
}
// function to print the linked list
console.log("The given linked list is empty");
}
else{
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
}
console.log(ans)
}
return tail.next = new Node(data);
}
// function to remove the duplicate numbers
return null;
}
var next = null;
var prev = null;
var cnt = 0;
while (cnt < number &&  cur != null){
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
cnt++;
}
if (next != null){
}
return prev;
}
// given number
var number = 3
console.log("The given linked list is: ")
// calling function to reverse elements in group
console.log("The Linked list after reversing elements in groups of 3 elements is: ")


## Conclusion

In this tutorial, we have implemented a JavaScript program to rotate the given linked list in subgroups of given length. We have implemented two approaches; one is recursive and another one is iterative. Both the approaches works on O(N) time complexity.

Updated on: 12-Apr-2023

87 Views